Java solution O(n)


  • 0
    S
        public String reverseVowels(String s) {
            if(s.length()==0){
                return "";
            }
            ArrayList<String> charArr = new ArrayList<String>();
            int j =0;
            StringBuilder str = new StringBuilder();
            String v= "aeiouAEIOU";
            for (int i =0; i<s.length(); i++){
                if(v.contains(s.charAt(i)+""))
                {
                    charArr.add(s.charAt(i)+"");
                    j++;
                }
            }
            int k =0;
            for(int i =0; i<s.length(); i++){
                 if(v.contains(s.charAt(i)+"")){
                     str.append(charArr.get(charArr.size()-1-k));
                     k++;
                   
                } else 
                str.append(s.charAt(i)+"");
            }
            return str.toString();
        }
    }

Log in to reply
 

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