-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsort-vowels-by-frequency.cpp
More file actions
38 lines (36 loc) · 1.1 KB
/
sort-vowels-by-frequency.cpp
File metadata and controls
38 lines (36 loc) · 1.1 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
// Time: O(n)
// Space: O(1)
// freq table, sort
class Solution {
public:
string sortVowels(string s) {
static const string VOWELS = "aeiou";
unordered_map<char, vector<int>> cnt;
for (int i = 0; i < size(s); ++i) {
if (VOWELS.find(s[i]) == string::npos) {
continue;
}
if (!cnt.count(s[i])) {
cnt[s[i]] = vector<int>{0, i};
}
++cnt[s[i]][0];
}
vector<tuple<char, int, int>> sorted_cnt;
for (const auto& [k, v] : cnt) {
sorted_cnt.emplace_back(k, v[0], v[1]);
}
ranges::sort(sorted_cnt, [](const auto& a, const auto& b) {
return get<1>(a) == get<1>(b) ? get<2>(a) > get<2>(b) : get<1>(a) < get<1>(b);
});
for (int i = 0; i < size(s); ++i) {
if (VOWELS.find(s[i]) == string::npos) {
continue;
}
s[i] = get<0>(sorted_cnt.back());
if (!(--get<1>(sorted_cnt.back()))) {
sorted_cnt.pop_back();
}
}
return s;
}
};