Java solution using Stack

  • 2
        public String reverseVowels(String s) {
            StringBuilder res = new StringBuilder(s);
            Stack<Character> stack= new Stack<>();
            for (char c: s.toCharArray()){
                if (c =='a' || c =='e'||c =='i'||c =='o'||c =='u'
                ||c =='A' || c =='E'||c =='I'||c =='O'||c =='U'){
            for (int i=0; i<s.length(); i++){
                if (s.charAt(i) =='a' || s.charAt(i) =='e'||s.charAt(i) =='i'||s.charAt(i) =='o'||s.charAt(i) =='u'
                ||s.charAt(i) =='A' || s.charAt(i) =='E'||s.charAt(i) =='I'||s.charAt(i) =='O'||s.charAt(i) =='U'){
                    res.setCharAt(i, stack.pop());
            return res.toString();

Log in to reply

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