-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC-Q-345-Reverse Vowels of a String.cpp
More file actions
57 lines (47 loc) · 1.16 KB
/
LC-Q-345-Reverse Vowels of a String.cpp
File metadata and controls
57 lines (47 loc) · 1.16 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
57
/*
Given a string s, reverse only all the vowels in the string and return it.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
Example 1:
Input: s = "IceCreAm"
Output: "AceCreIm"
Explanation:
The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".
Example 2:
Input: s = "leetcode"
Output: "leotcede"
Constraints:
1 <= s.length <= 3 * 105
s consist of printable ASCII characters.
*/
//SOLUTION
class Solution {
public:
bool isVowel(char a)
{
if(a=='a' || a=='e' || a=='i' || a=='o' || a=='u' || a=='A' || a=='E' || a=='I' || a=='O' || a=='U')
return true;
return false;
}
string reverseVowels(string s) {
int i=0,j=s.size()-1;
while(i<j)
{
if(isVowel(s[i]) && isVowel(s[j]))
{
swap(s[i],s[j]);
i++;
j--;
}
else if(isVowel(s[i]))
j--;
else if(isVowel(s[j]))
i++;
else
{
i++;
j--;
}
}
return s;
}
};