Java Solution using two pointers, easy to understand..


  • 1
    public class Solution {
    public String reverseVowels(String s) {
        char[] c=s.toCharArray();
        int l=0;
        int r=s.length()-1;
        boolean lres,rres;
        while(l<r){
            lres=isVowel(c[l]);
            rres=isVowel(c[r]);
            if(lres&&rres){
                swap(c,l,r);
                l++;
                r--;
            }
            else if(!lres){
                if(rres){
                    l++;
                }
                else{
                    l++;
                    r--;
                }
            }
            else{
                r--;
            }
        }
        return String.valueOf(c);
    }
    private void swap(char[] c, int i, int j){
        char tmp;
        tmp=c[i];
        c[i]=c[j];
        c[j]=tmp;
        
    }
    private boolean isVowel(char c){
      char cl=Character.toLowerCase(c);
      return cl=='a' ||cl=='e' ||cl=='i' ||cl=='o'||cl=='u';
    
    }
    

    }


  • 0
    H

    You have to consider uppercase issues. A E I O U


  • 0

    char cl=Character.toLowerCase(c);
    has been covered.


Log in to reply
 

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