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".
Subscribe to see which companies asked this question
1 public class Solution { 2 public String reverseVowels(String s) { 3 if(null == s) return s; 4 char[] tmp = s.toCharArray(); 5 Stack<Character> stack = new Stack<Character>(); 6 for(int i = 0; i<tmp.length; i++){ 7 if(isVowels(tmp[i])){ 8 stack.push(tmp[i]); 9 } 10 } 11 for(int i = 0; i<tmp.length; i++){ 12 if(isVowels(tmp[i])){ 13 tmp[i] = stack.pop(); 14 } 15 } 16 String res = new String(tmp); 17 return res; 18 } 19 20 public boolean isVowels(char c){ 21 if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ){ 22 return true; 23 } 24 return false; 25 }