6ms java solution


  • 0
    T

    public String reverseVowels(String s) {
    if(s.length()==0||s.length()==1)return s;
    char[] in = s.toCharArray();
    char[] ha = new char[s.length()];
    int[] he = new int[s.length()];
    int count = 0;
    for(int i = 0 ; i < s.length() ; i++){
    if(in[i]=='a'||in[i]=='e'||in[i]=='i'||in[i]=='o'||in[i]=='u'
    ||in[i]=='A'||in[i]=='E'||in[i]=='I'||in[i]=='O'||in[i]=='U'){
    ha[count]=in[i];
    he[count]=i;
    count+=1;
    }
    }
    for(int i = 0 ; i < count ; i++){
    in[he[i]]=ha[count-1-i];
    }
    return new String(in);
    }


  • 0
    T

    Basically, the process is as below:
    Run through the string, find the vowels' locations and what exactly they are. Put two results into two arrays, which is type of char and int, respectively. call the sum of vowels count. Then in place of int[i] of the string, we use char[count-i-1] to replace the previous vowel.


Log in to reply
 

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