Two pointers inner cycle as O(n);


  • 1
    W
    public class Solution {
        private static boolean[] chars = new boolean[256];
    
        static {
            chars['a'] = true;
            chars['e'] = true;
            chars['i'] = true;
            chars['o'] = true;
            chars['u'] = true;
            chars['A'] = true;
            chars['E'] = true;
            chars['I'] = true;
            chars['O'] = true;
            chars['U'] = true;
        }
        
        public static String reverseVowels(String s) {
            byte[] bytes = s.getBytes();
            int start = 0, end = bytes.length - 1;
            byte b;
            while (start < end) {
                if (chars[bytes[start]]) {
                    while (end > start) {
                        if (chars[bytes[end]]) {
                            b = bytes[start];
                            bytes[start] = bytes[end];
                            bytes[end] = b;
                            end--;
                            break;
                        }
                        end--;
                    }
                }
                start++;
            }
            return new String(bytes);
        }
    }
    

    Nothing new, easy as first step;


Log in to reply
 

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