给定一个字符串,请将字符串里的字符按照出现的频率降序排列。
class Solution: def frequencySort(self, s: str) -> str: d={} res='' for i in s: d[i]=d.get(i,0)+1 d=sorted(d.items(),key=lambda k:-k[1])#字典排序 for i in d: for j in range(i[1]): res+=i[0] return res
2024-03-28 23:32:28
给定一个字符串,请将字符串里的字符按照出现的频率降序排列。
class Solution: def frequencySort(self, s: str) -> str: d={} res='' for i in s: d[i]=d.get(i,0)+1 d=sorted(d.items(),key=lambda k:-k[1])#字典排序 for i in d: for j in range(i[1]): res+=i[0] return res