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".
给定一个字符串,将其中的元音字母逆置。
public String reverseVowels(String s) {
if (s.trim().length() <= 1) return s;
int left = 0, right = s.length() - 1;
char[] snew = new char[s.length()];
HashSet<Character> set = new HashSet<>(Arrays.asList('A', 'E', 'I', 'O', 'U', 'a', 'e', 'i',
'o', 'u'));
while (left <= right) {
if (!set.contains(s.charAt(left))) snew[left] = s.charAt(left++);
if (!set.contains(s.charAt(right))) snew[right] = s.charAt(right--);
if (set.contains(s.charAt(left)) && set.contains(s.charAt(right))) {
snew[left] = s.charAt(right);
snew[right--] = s.charAt(left++);
}
}
return new String(snew);
}