题目:
方法:
遍历字符串s到字典中,key为s中出现的字符,value为出现次数
随后遍历字典,每次将最大值输出value次
依次将最大值输出至result中
返回result,结束
class Solution:
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
dict1 = {}
result = str()
for i in range(len(s)):
if s[i] not in dict1:dict1[s[i]] = 1
else:dict1[s[i]] += 1
while len(dict1) > 0:
m = max(dict1,key=dict1.get)
result += m*dict1[m]
del dict1[m]
return result