-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseVowels.cpp
More file actions
51 lines (48 loc) · 1.33 KB
/
reverseVowels.cpp
File metadata and controls
51 lines (48 loc) · 1.33 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
/*
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".
*/
//my Solution
class Solution {
public:
string reverseVowels(string s) {
stack<char> sc;
for(char x:s)
if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u'
||x=='A'||x=='E'||x=='I'||x=='O'||x=='U')
sc.push(x);
for(int i=0;i!=s.length();i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'
||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
{
s[i]=sc.top();
sc.pop();
}
}
return s;
}
};
//other Solution
class Solution {
public:
string reverseVowels(string s) {
int dict[256] = {0};
dict['a'] = 1, dict['A'] = 1;
dict['e'] = 1, dict['E'] = 1;
dict['i'] = 1, dict['I'] = 1;
dict['o'] = 1, dict['O'] = 1;
dict['u'] = 1, dict['U'] = 1;
int start = 0, end = (int)s.size() - 1;
while(start < end){
while(start < end && dict[s[start]] == 0) start++;
while(start < end && dict[s[end]] == 0) end--;
swap(s[start],s[end]);
start++;end--;
}
return s;
}
};