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".
Thinking of solving problems
Two pointers, one moving forward, one moving backwards, and the other to find the vowel letters.
Implementation code
//Runtime:12 MS Public class solution { Private Static FinalString STR ="Aeiouaeiou";Private Static Finalset<character> vowels =NewHashset<character> (Str.length ());Static{ for(inti =0; I < str.length (); i++) {Vowels.add (Str.charat (i)); } } PublicStringReversevowels(String s) {StringBuilder SB =NewStringBuilder (s);intleft =0;intright = Sb.length ()-1; while(Left < right) { while(Left < right &&!isvowels (Sb.charat (left))) {++left; } while(Left < right &&!isvowels (Sb.charat)) {--right; }Chartemp = Sb.charat (left); Sb.setcharat (left++, Sb.charat (right)); Sb.setcharat (right--, temp); }returnSb.tostring (); }Private Boolean Isvowels(CharCH) {returnVowels.contains (CH); }}
[Leetcode] Reverse vowels of a String