Compact No Inner-loop Solution


  • 0
    public String reverseVowels(String s) {
        int len = s.length();
        char [] str = s.toCharArray();
        int l = 0, r = len - 1;
        while(l < r) {
            if(!isVow(str[l])) l ++;
            else if(!isVow(str[r])) r --;
            else swap(str, l ++, r --);
        }
        return new String(str);
    }
    
    private void swap(char [] arr, int i, int j) {
        char tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
    
    private boolean isVow(char 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.