Skip to content

Instantly share code, notes, and snippets.

@namtx
Created November 4, 2022 05:04
Show Gist options
  • Save namtx/3a2a50fb520e119a8c2bcd5f8e8a29aa to your computer and use it in GitHub Desktop.
Save namtx/3a2a50fb520e119a8c2bcd5f8e8a29aa to your computer and use it in GitHub Desktop.
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