13 ms Java Solution


  • 0
    C

    13 ms solution

    public String reverseVowels(String s) {
            Set<Character> vowles = new HashSet<Character>();
            vowles.add('a');
            vowles.add('e');
            vowles.add('i');
            vowles.add('o');
            vowles.add('u');
            vowles.add('A');
            vowles.add('E');
            vowles.add('I');
            vowles.add('O');
            vowles.add('U');
            
            char[] ch = s.toCharArray();
            int i = 0;
            int j = ch.length - 1;
            
            while(i<j){
                if(vowles.contains(ch[i]) && vowles.contains(ch[j])){
                    char tmp = ch[j];
                    ch[j] = ch[i];
                    ch[i] = tmp;
                    i++;
                    j--;
                }else if(vowles.contains(ch[i])){
                    j--;
                }else if(vowles.contains(ch[j])){
                    i++;
                }else{
                    i++;
                    j--;
                }
            }
            
            return new String(ch);
        }
    

Log in to reply
 

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