Created
November 4, 2022 05:04
-
-
Save namtx/3a2a50fb520e119a8c2bcd5f8e8a29aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.namtx.leetcode.daily; | |
public class ReverseVowelsOfAString { | |
public static void main(String[] args) { | |
System.out.println(new ReverseVowelsOfAString().reverseVowels("aA")); | |
} | |
public String reverseVowels(String s) { | |
char[] chars = s.toCharArray(); | |
int left = 0; | |
int right = s.length() - 1; | |
while (left < right) { | |
if (isVowel(chars[left])) { | |
if (isVowel(chars[right])) { | |
// swap | |
char tmp = chars[left]; | |
chars[left] = chars[right]; | |
chars[right] = tmp; | |
left++; | |
} | |
right--; | |
} else { | |
left++; | |
} | |
} | |
StringBuilder sb = new StringBuilder(); | |
for (char c : chars) { | |
sb.append(c); | |
} | |
return sb.toString(); | |
} | |
private boolean isVowel(char c) { | |
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment