My 4ms Java solutionn


  • 0
    S
    public class Solution {
        public String reverseVowels(String s) {
            if (s == null) return null;
            if (s.equals("")) return s;
            char[] arr = s.toCharArray();
            for (int i = 0, j = arr.length-1; i < j;) {
                if (isVowels(arr[i]) && isVowels(arr[j])) {
                    char temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                    i++;
                    j--;
                } else if (isVowels(arr[i])) {
                    j--;
                } else if (isVowels(arr[j])) {
                    i++;
                } else {
                    i++;
                    j--;
                }
            }
            return new String(arr);
        }
        
        public static boolean isVowels(char ch) {
            return ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' 
                || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O'
                || ch == 'u' || ch == 'U';
        }
    }

Log in to reply
 

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