Beats ~90% in Java


  • 0
    L
    public class Solution {
        private boolean contains(char c) {
            return  (c^'a')==0 || (c^'A')==0 || 
                    (c^'e')==0 || (c^'E')==0 ||
                    (c^'i')==0 || (c^'I')==0 ||
                    (c^'o')==0 || (c^'O')==0 ||
                    (c^'u')==0 || (c^'U')==0 ;
        }
        public String reverseVowels(String s) {
            if(s==null) {
                return s;
            }
            char[] arr = s.toCharArray();
            for(int i=0,j=s.length()-1;j>i;) {
                while(i<s.length()&& !contains(arr[i])) {
                    i++;
                }
                while(j>=0 && !contains(arr[j])) {
                    j--;
                }
                if(j>i) {
                    arr[i] ^=arr[j];
                    arr[j] ^=arr[i];
                    arr[i] ^=arr[j];
                    i++;
                    j--;
                }
            }
            return new String(arr);
        }
    }
    

Log in to reply
 

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