Java 2-pointer solution


  • 0
    public class Solution {
        public String reverseVowels(String s) {
            final String vowels = "aAeEiIoOuU";
            char[] arr = s.toCharArray();
            int start = 0;
            int end = arr.length - 1;
            while(start < end) {
                while(start < arr.length && vowels.indexOf(arr[start]) == -1){
                    start++;
                }
                while(end >= 0 && vowels.indexOf(arr[end]) == -1){
                    end--;
                }
                if(start < end) {
                    char c = arr[start];
                    arr[start] = arr[end];
                    arr[end] = c;
                    start++;
                    end--;
                }
            }
            return new String(arr);
        }
    }
    

Log in to reply
 

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