FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solution/C++/sort-characters-by-frequency.cpp at master · paperfactory/LeetCode-Solution · GitHub
paperfactory
LeetCode-Solution
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solution
/
C++
/
sort-characters-by-frequency.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
26 lines (22 loc) · 621 Bytes
Breadcrumbs
LeetCode-Solution
/
C++
/
sort-characters-by-frequency.cpp
Copy path
File metadata and controls
26 lines (22 loc) · 621 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
Time: O(n)
//
Space: O(n)
class
Solution
{
public:
string
frequencySort
(string s) {
unordered_map<
char
,
int
> freq;
for
(
const
auto
& c : s) {
++freq[c];
}
vector<string>
counts
(s.
size
() +
1
);
for
(
const
auto
& kvp : freq) {
counts[kvp.
second
].
push_back
(kvp.
first
);
}
string result;
for
(
int
count = counts.
size
() -
1
; count >=
0
; --count) {
for
(
const
auto
& c : counts[count]) {
result +=
string
(count, c);
}
}
return
result;
}
};
Back
|
FazBrowse Home
|
New Git URL