Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1: Given s = "hello", return "holle".

Example 2: Given s = "leetcode", return "leotcede".

Note: The vowels does not include the letter "y".

Solution:
public class Solution {
    public String reverseVowels(String s) {
        char[] c = s.toCharArray();
        int i = 0, j = c.length-1,left = -1, right = -1;
        while(i < j){
            int t1 = Character.toLowerCase(c[i]);
            int t2 = Character.toLowerCase(c[j]);
            if(t1 == 'a'||t1 == 'e'||t1 == 'i'||t1 == 'o'||t1 == 'u'){
                if(t2 == 'a'||t2 == 'e'||t2 == 'i'||t2 == 'o'||t2 == 'u'){
                    char temp = c[i];
                    c[i] = c[j];
                    c[j] = temp;
                    i++;
                    j--;
                    continue;
                }
                j--;
            }
            else i++;
        }
        return new String(c);
    }
}

results matching ""

    No results matching ""