Title Link: https://leetcode.com/problems/reverse-vowels-of-a-string/
Topic:
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".
Ideas:
Easy
Algorithm:
Public String Reversevowels (string s) {char c[] = S.tochararray (); set<string> vowels = new hashset<string> (); Vowels.add ("a"); Vowels.add ("E"); Vowels.add ("I"); Vowels.add ("O"); Vowels.add ("U"); Vowels.add ("A"); Vowels.add ("E"); Vowels.add ("I"); Vowels.add ("O"); Vowels.add ("U"); int v1=-1,v2=-1; for (int start=0,end=c.length-1;start<end;) {if (v1==-1) {if (Vowels.contains (c[start]+ "")) V1 = start; else start++; } if (V2==-1) {if (Vowels.contains (c[end]+ ")) v2 = end; else end--; } if (v1!=-1&&v2!=-1) {char tmp = C[V1]; C[V1] = C[v2]; C[V2] = tmp; V1 =-1; v2 =-1; start++; end--; } } return new String (c); }
"Leetcode" Reverse vowels of a String