o(n) Java solution


  • 0
    H

    '''
    class Solution {
    public String reverseVowels(String s) {
    int i = 0;
    int j = s.length() -1;
    char temp;
    String vowels = "aeiouAEIOU";
    StringBuilder s2 = new StringBuilder(s);
    while(i < j){
    if(vowels.indexOf(s.charAt(i)) != -1){ //char at i is a vowel
    if(vowels.indexOf(s.charAt(j)) != -1){
    //s2 = swap(s2,i,j);
    s2.setCharAt(i,s.charAt(j));
    s2.setCharAt(j,s.charAt(i));
    i++;
    j--;
    continue;
    }
    j--;
    }
    else if(vowels.indexOf(s.charAt(j)) != -1)
    i++;
    else{
    i++;
    j--;
    }
    }
    return s2.toString();
    }
    }
    '''


Log in to reply
 

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