Java 5ms Solution


  • 0
    B
    public String reverseVowels(String s) {
        if(s.equals(""))
            return "";
        char[] arr = s.toCharArray();
        int end = arr.length - 1;
        for(int i = 0; i < arr.length - 1; i++) {
            if(i > end) 
                return new String(arr);
            if("aeiouAEIOU".indexOf(arr[i]) == -1)
                continue;
            while("aeiouAEIOU".indexOf(arr[end]) == -1)
                --end;
            if(arr[end] != arr[i]) {
                char temp = arr[i];
                arr[i] = arr[end];
                arr[end] = temp;
            }
            --end;
        }
        return new String(arr);
    }

Log in to reply
 

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