-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseVowelsOfString.cpp
More file actions
42 lines (38 loc) · 905 Bytes
/
reverseVowelsOfString.cpp
File metadata and controls
42 lines (38 loc) · 905 Bytes
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
/**
*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".
*/
class Solution {
public:
string reverseVowels(string s) {
string str(s);
int len = s.size();
if(len == 0)
return s;
int start = 0;
int last = len - 1;
while(start < last)
{
while(start < last && !isVowels(s[start]))
start++;
while(start < last && !isVowels(s[last]))
last--;
char temp = str[start];
str[start] = str[last];
str[last] = temp;
start++;
last--;
}
return str;
}
bool isVowels(char c)
{
if(c == 'a' || c=='o' ||c == 'e' || c == 'i' || c== 'u'|| c == 'A' || c == 'O' || c == 'E' || c == 'U' || c == 'I')
return true;
else
return false;
}
};