Clean, easy to follow Java solution


  • 0
    D
    class Solution {
        public String reverseVowels(String s) {
            String vowels = "aeiouAEIOU";
            StringBuilder sb = new StringBuilder(s);
            int j = s.length() - 1, i = 0;
            
            while(i < j)    {
                if(vowels.indexOf(s.charAt(i)) >= 0 && vowels.indexOf(s.charAt(j)) >= 0) {
                    sb.setCharAt(i, s.charAt(j));
                    sb.setCharAt(j, s.charAt(i));
                    ++i; --j;
                }
                else if(vowels.indexOf(s.charAt(i)) < 0)
                        ++i;
                else if(vowels.indexOf(s.charAt(j)) < 0)
                        --j;
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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