List and O(n) java


  • 0
    D
     public static final List<Character> list = Arrays.asList('a','e','i','o','u','A','E','I','O','U');
        
        public String reverseVowels(String s) {
            char sArr[] = s.toCharArray();
            int i = 0;
            int j = sArr.length-1;
            while(i<j) {
                if(list.contains(sArr[i]) && list.contains(sArr[j])) {
                    char ch = sArr[i];
                    sArr[i] = sArr[j];
                    sArr[j] = ch;
                    i++; j--;
                } else if(list.contains(s.charAt(i))) {
                    j--;
                } else {
                    i++;
                }
            }
            return new String(sArr);
        }
    
    

Log in to reply
 

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