Simple Java Solution, One Line HashSet Init


  • 1
    H
    public class Solution {
        public String reverseVowels(String s) {
            Set<Character> set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
            char[] arr = s.toCharArray();
            int left = 0, right = arr.length - 1;
            while (left < right) {
                if (!set.contains(arr[left])) {
                    left++;
                } else if (!set.contains(arr[right])) {
                    right--;
                } else {
                    char tmp = arr[left];
                    arr[left] = arr[right];
                    arr[right] = tmp;
                    left++;
                    right--;
                }
            }
            return new String(arr);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.