Java 2 Pointers clean solution


  • 0
    R
        char[] vow = {'a','A','e','E','i','I','o','O','u','U'};
        public String reverseVowels(String s) {
            char[] res = new char[s.length()];
            int left = 0,right = s.length()-1;
            while(left <= right)
            {
                while(left < right && !isVowel(s.charAt(left)))
                {
                    res[left] = s.charAt(left);
                    left++;
                }
                while(right > left && !isVowel(s.charAt(right)))
                {
                    res[right] = s.charAt(right);
                    right--;
                }
                if(left <= right)
                {
                    res[left] = s.charAt(right);
                    res[right] = s.charAt(left);
                    left++;
                    right--;
                }
            }
            return new String(res);
        }
        
        private boolean isVowel(char c)
        {
            for(char ch:vow)
                if(ch == c)
                    return true;
            return false;
        }
    

Log in to reply
 

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