Want to share mine clean java solution using array as hash table, beat 95%


  • 0
    Z

    guess no need to explain anymore.

    public String reverseVowels(String s) {
            char[] dict = new char[]{'a','e','i','o','u', 'A','E','I','O','U'};
            int[] all = new int[256];
            for (char c : dict) {
                all[c]++;
            }
            char[] chars = s.toCharArray();
            int n = chars.length;
            int l = 0, r = n - 1;
            while (l < r) {
                while (l < r && all[chars[l]] == 0) l++;
                while (l < r && all[chars[r]] == 0) r--;
                char temp = chars[l];
                chars[l] = chars[r];
                chars[r] = temp;
                l++; r--;
            }
            return String.valueOf(chars);
        }
    

Log in to reply
 

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