-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode345_reverse_vowels_of_a_string.cpp
63 lines (52 loc) · 1.32 KB
/
leetcode345_reverse_vowels_of_a_string.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.cpp
*
* @author Wang Guibao ([email protected])
* @date 2024/04/03 14:36
* @brief https://leetcode.com/problems/reverse-vowels-of-a-string
*/
#include <iostream>
#include <set>
#include <vector>
using namespace std;
class Solution {
public:
string reverseVowels(string s) {
std::string result = s;
std::set<char> dict {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
std::vector<char> chars;
std::vector<int> pos;
for (int i = 0; i < s.length(); ++i) {
if (dict.find(s[i]) != dict.end()) {
chars.push_back(s[i]);
pos.push_back(i);
}
}
int b = 0;
int e = pos.size() - 1;
while (b < e) {
int temp = pos[b];
pos[b] = pos[e];
pos[e] = temp;
++b;
--e;
}
for (int i = 0; i < pos.size(); ++i) {
result[pos[i]] = chars[i];
}
return result;
}
};
int main() {
while (1) {
std::string word;
std::cout << "Input word: ";
if (!std::getline(std::cin, word)) {
return 0;
}
Solution solution;
auto ret = solution.reverseVowels(word);
std::cout << ret << std::endl;
}
return 0;
}