1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Solution { private static final Set<Character> VOWELS = new HashSet<>(Arrays.asList('a' , 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'));
public String reverseVowels(String s) { char[] chars = s.toCharArray(); int i = 0, j = chars.length - 1; while (i < j) { while (i < j && !VOWELS.contains(chars[i])) { i++; } while (i < j && !VOWELS.contains(chars[j])) { j--; } swap(chars, i++, j--); }
return new String(chars); }
private void swap(char[] chars, int i, int j) { char tmp = chars[i]; chars[i] = chars[j]; chars[j] = tmp; } }
|