java beats 95% easy solution


  • 0
    Z

    public class Solution {
    public String reverseVowels(String s) {
    char[] chars = s.toCharArray();
    char temp;
    int i = 0, j = chars.length - 1;

        while(i < j) {
            while(i < j && !vowel(chars[i])) 
                i++;
            while(i < j && !vowel(chars[j])) 
                j--;
            if(i < j) {
                temp = chars[i];
                chars[i] = chars[j];
                chars[j] = temp;
                i++;
                j--;
            }
        }
        
        return new String(chars);
    }
    
    public boolean vowel(char c) {
        
        switch(c){
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                return true;
            default:
                return false;
        }
    }
    

    }


Log in to reply
 

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