Skip to content

Commit

Permalink
Time: 77 ms (53.55%) | Memory: 56.8 MB (21.96%) - LeetSync
Browse files Browse the repository at this point in the history
  • Loading branch information
ShatilKhan committed Feb 7, 2024
1 parent 84a7f0d commit d881cf6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 451-sort-characters-by-frequency/sort-characters-by-frequency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {string} s
* @return {string}
*/
var frequencySort = function(s) {
const counter = new Map();
for (const char of s) {
counter.set(char, (counter.get(char) || 0) + 1);
}

const pq = Array.from(counter.entries());
pq.sort((a, b) => b[1] - a[1]);

let result ='';

for(const [char, freq] of pq) {
result += char.repeat(freq);
}
return result;
};

0 comments on commit d881cf6

Please sign in to comment.