LeetCode 345. Reverse Vowels of a String
Description
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = “hello”, return “holle”.
Example 2:
Given s = “leetcode”, return “leotcede”.
class Solution {
public String reverseVowels(String s) {
char[] str = s.toCharArray();
String vol = "aeiouAEIOU";
int lo = 0;
int hi = str.length - 1;
while (lo < hi) {
while (lo < hi && !vol.contains(str[lo]+"")) {
lo++;
}
while (lo < hi && !vol.contains(str[hi] + "")) {
hi--;
}
char tmp = str[lo];
str[lo] = str[hi];
str[hi] = tmp;
lo++;
hi--;
}
return new String(str);
}
}