Skip to content

Latest commit

 

History

History
19 lines (15 loc) · 543 Bytes

2022-12-03.md

File metadata and controls

19 lines (15 loc) · 543 Bytes

题目

  1. Sort Characters By Frequency

Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.

Return the sorted string. If there are multiple answers, return any of them.

思路

代码

var frequencySort = function(s) {
  const map = {}
  for (let i = 0; i < s.length; i++) {
    map[s[i]] = (map[s[i]] || '') + s[i]
  }
  return Object.values(map).sort((a, b) => b.length - a.length).join('')
};