Java Concise Solution


  • 0
        private final Set<Character> set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
        public String reverseVowels(String s) {
            if (s == null || s.length() == 0) return "";
            char[] arr = s.toCharArray();
            int i = 0;
            int j = s.length() - 1;
            while (i < j) {
                while (i < j && !set.contains(arr[i])) i++;
                while (i < j && !set.contains(arr[j])) j--;
                if (i >= j) break;
                swap(arr, i, j);
                i++;
                j--;
            }
            return new String(arr);
        }
        private void swap(char[] arr, int i, int j) {
            char temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    

Log in to reply
 

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