345. Reverse Vowels of a String
class Solution {
public String reverseVowels(String s) {
int len = s.length();
int left = 0;
int right = len - 1;
char c[] = s.toCharArray();
while (left < right) {
while (left < right && !isVowel(c[left])) {
left++;
}
while (left < right && !isVowel(c[right])) {
right--;
}
if (left < right) {
swap(c, left, right);
}
left++;
right--;
}
return String.valueOf(c);
}
private void swap(char[] c, int left, int right) {
char temp = c[left];
c[left] = c[right];
c[right] = temp;
}
private boolean isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}
}
Last updated