Write a function that takes a string as input and reverse only the vowels of a string.
Detail instruction can be found here.
Example 1:
Input: "hello"
Output: "holle"
Example 2:
Input: "leetcode"
Output: "leotcede"
Approach: Two Pointers
public String reverseVowels(String s) {
char[] chars = s.toCharArray();
int lo = 0, hi = s.length() - 1;
while (lo < hi) {
if (isVowel(chars[lo]) && isVowel(chars[hi])) {
char t = chars[lo];
chars[lo] = chars[hi];
chars[hi] = t;
lo++; hi--;
}
if (!isVowel(chars[lo]))
lo++;
if (!isVowel(chars[hi]))
hi--;
}
return new String(chars);
}
private helper methods
public boolean isVowel(char c) {
return "AEIOUaeiou".indexOf(c) != -1;
}
This is only for discussion and communication. Please don't use this for submission of assignments.