-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReverseVowels.cpp
More file actions
56 lines (51 loc) · 1.18 KB
/
ReverseVowels.cpp
File metadata and controls
56 lines (51 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
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".
*/
class Solution {
public:
bool isVowel(char inp){
switch(inp){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
return true;
default:
return false;
}
}
string reverseVowels(string s) {
int f = 0;
int t = s.length();
//unordered_set<char> vowels ( {'a','e','i','o','u','A','E','I','O','U'} );
while(f<=t-1){
if(isVowel(s[f]) && isVowel(s[t]))
{
char temp = s[f];
s[f] = s[t];
s[t] = temp;
f++;
t--;
}
if(!isVowel(s[f])){
f++;
}
if(!isVowel(s[t])){
t--;
}
}
return s;
}
};