Java Human Readable Solution with two pointer approach: 5ms


  • 0
    public class Solution {
        public static boolean checkVowel(char c){
            if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')return true;
            if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')return true;
            return false;
        }
        public static String reverseVowels(String s) {
            char[] s_ar = s.toCharArray();
            int start=0;
            int end=s.length()-1;
            while(start<end){
                if(checkVowel(s_ar[start])==false)start++;
                if(checkVowel(s_ar[end])==false)end--;
                if((checkVowel(s_ar[start])==true)&&(checkVowel(s_ar[end])==true)){
                    char tmp = s_ar[start];
                    s_ar[start] = s_ar[end];
                    s_ar[end] = tmp;
    				start++;
    				end--;
                }
            }
            return new String(s_ar);
        }
    }
    

Log in to reply
 

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