Java 7ms solution


  • 0
    S
    public String reverseVowels(String s) {
    	if (s == null || s.length() == 0) return s;
    	char[] array = s.toCharArray();
    	int a = 0;
    	int b = array.length-1;
    	char temp;
    	while (a<b) {
    		if (isVowel(array[a]) && isVowel(array[b])) {
    			temp = array[a];
    			array[a] = array[b];
    			array[b] = temp;
    			a++;
    			b--;
    		}
    		if (!isVowel(array[a])) a++;
    		if (!isVowel(array[b])) b--;
    	}
    	return String.valueOf(array);
    }
    
    private boolean isVowel(char c) {
    	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ) return true;
    	return false;
    }
    

Log in to reply
 

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