Java 4ms, beats 95.9% of solutions.


  • 0
    B
        public String reverseVowels(String s) {
            char[] arr=s.toCharArray();
            int len=s.length();
            int left=0,right=len-1;
            while(left<right){
                while(!isVowel(arr[left])&&left<len-1)left++;
                while(!isVowel(arr[right])&&right>0)right--;
                if(left<right){
                    char temp=arr[left];
                    arr[left]=arr[right];
                    arr[right]=temp;
                    left++;right--;
                }else{
                    break;
                }
            }
            return new String(arr);
        }
        public boolean isVowel(char c){
            if(c=='A'||c=='a'||c=='E'||c=='e'||c=='I'||
            c=='i'||c=='O'||c=='o'||c=='U'||c=='u')return true;
            return false;
        }```

Log in to reply
 

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