My simple Java solution


  • 0
    N
    public class Solution {
        public String reverseVowels(String s) {
            char[] c = s.toCharArray();
            int i = 0, j = s.length() - 1;
            while(i < j){
                while(i < j && !isVowel(c[i])){
                    i++;
                }
                while(i < j && !isVowel(c[j])){
                    j--;
                }
                if(i < j){
                    char ch = c[i];
                    c[i++] = c[j];
                    c[j--] = ch;
                }
            }
            return String.valueOf(c);
        }
        public boolean isVowel(Character c){
            return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
        }
    }
    

Log in to reply
 

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