Skip to content

Commit 9d6287b

Browse files
committedJan 12, 2020
Add solution #451
1 parent f832ec7 commit 9d6287b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 451. Sort Characters By Frequency
3+
* https://leetcode.com/problems/sort-characters-by-frequency/
4+
* Difficulty: Medium
5+
*
6+
* Given a string, sort it in decreasing order
7+
* based on the frequency of characters.
8+
*/
9+
10+
/**
11+
* @param {string} s
12+
* @return {string}
13+
*/
14+
var frequencySort = function(s) {
15+
const map = new Map();
16+
s.split('').forEach(char => {
17+
map.set(char, map.has(char) ? map.get(char) + 1 : 1);
18+
});
19+
20+
return [...map]
21+
.sort((a, b) => b[1] - a[1])
22+
.map(entry => entry[0].repeat(entry[1]))
23+
.join('');
24+
};

0 commit comments

Comments
 (0)
Please sign in to comment.