Java solution using Stack


  • 1
    L
    public String reverseVowels(String s) {
        char[] chars = s.toCharArray();
        Stack<String> rev = new Stack<>();
        String vowels = "aeiouAEIOU";
        for(char c:chars){
            if(vowels.contains(c+""))
                rev.push(c+"");
        }
        for(int i=0;i<chars.length;i++){
            if(vowels.contains(chars[i]+""))
                chars[i] = rev.pop().charAt(0);
        }
        return new String(chars);
    }

  • 0
    This post is deleted!

Log in to reply
 

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