One pass Java solution, easy to understand!


  • 0
    H
    public String reverseVowels(String s) {
    
        String vowel = "aeiouAEIOU";
        char[] c = s.toCharArray();
        int i=0,j=c.length-1;
        while(i<j){
            if(vowel.contains(Character.toString(c[i]))&&vowel.contains(Character.toString(c[j]))){
                char tmp = c[i];
                c[i++] = c[j];
                c[j--] = tmp;
            }
            if(!vowel.contains(Character.toString(c[i]))) i++;
            if(!vowel.contains(Character.toString(c[j]))) j--;
        }
        return new String(c);
    }

Log in to reply
 

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