-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2785.cpp
More file actions
47 lines (47 loc) · 1.32 KB
/
2785.cpp
File metadata and controls
47 lines (47 loc) · 1.32 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
class Solution {
public:
int getIndex(char c) {
if (c == 'A') return 0;
if (c == 'E') return 1;
if (c == 'I') return 2;
if (c == 'O') return 3;
if (c == 'U') return 4;
if (c == 'a') return 5;
if (c == 'e') return 6;
if (c == 'i') return 7;
if (c == 'o') return 8;
if (c == 'u') return 9;
return -1;
}
char mapIndex(int index) {
if (index == 0) return 'A';
if (index == 1) return 'E';
if (index == 2) return 'I';
if (index == 3) return 'O';
if (index == 4) return 'U';
if (index == 5) return 'a';
if (index == 6) return 'e';
if (index == 7) return 'i';
if (index == 8) return 'o';
if (index == 9) return 'u';
return 'N';
}
string sortVowels(string s) {
vector<int> counts(10, 0);
for (auto& c : s) {
int index = getIndex(c);
if (index != -1) counts[index]++;
}
int n = s.size();
int current = 0;
for (int i = 0; i < n; ++i) {
int index = getIndex(s[i]);
if (index != -1) {
while (counts[current] == 0) current++;
s[i] = mapIndex(current);
counts[current] -= 1;
}
}
return s;
}
};