Reverse vowels of a string java


  • 0
    W
        public String reverseVowels(String s) {
            if(s.equals("")){
                return "";
            }
            char[] charArray = s.toCharArray();
            int end = charArray.length - 1;
            try{
                for(int start = 0; start < end; start++) {
                    if (isVowel(charArray[start])) {
                        for (int j = end; j > start; j--) {
                            if (isVowel(charArray[j])) {
                                char ch = charArray[start];
                                charArray[start] = charArray[j];
                                charArray[j] = ch;
                                end = j-1;
                                break;
                            }
                        }
                    }
                }
            }catch(Exception ex){
            }
        return new String(charArray);
        }
    
        public Boolean isVowel(char c){
            return c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U';
        }```

Log in to reply
 

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