/
345.cpp
63 lines (56 loc) · 960 Bytes
/
345.cpp
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
58
59
60
61
62
63
// leetcode345: reverse-vowels-of-a-string
#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
bool isVowel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
return true;
} else if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
return true;
} else {
return false;
}
}
string reverseVowels(string s) {
if (s == "") return s;
int l = 0;
int r = s.size()-1;
while (l <= r) {
if (!isVowel(s[l])) {
l++;
continue;
}
if (!isVowel(s[r])) {
r--;
continue;
}
string tmp = "";
tmp += s[l];
s[l] = s[r];
s[r] = tmp[0];
l++;
r--;
}
return s;
}
void printAns(string a) {
cout << reverseVowels(a) << endl;
}
void test() {
string a = "hello";
string a1 = "leetcode";
printAns(a);
printAns(a1);
}
int main() {
test();
return 0;
}