Easy-to-understand 5ms Java solution


  • 0
    Y
        public String reverseVowels(String s) {
            char[] array = s.toCharArray();
            int i = 0; int j = array.length-1;
            while (i<j){
                if (!isVowel(array[i])) i++;
                if (!isVowel(array[j])) j--;
                if (isVowel(array[i]) && isVowel(array[j])){
                    char temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;
                    i++;
                    j--;
                }
            }
            return new String(array);
        }
        
        public boolean isVowel(char c){
            c = Character.toLowerCase(c);
            if (c=='a' || c=='e' || c=='i' || c=='o' || 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.