题解 | #字符统计#

字符统计

http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

思路

  • 先把输入的s进行排序,这样排序一得到的就是ASCII的顺序,免除了后处理;
  • 再通过字典统计个数,对个数进行排序即可,这是排序二

代码

while True:
    try:
        s = input()
        s = sorted(s)
        counter = dict()
        for c in s:
            if c not in counter.keys():
                counter[c] = 1
            else:
                counter[c] += 1
        res_list = sorted(counter.items(), key=lambda x:x[1], reverse=True)
        flag = 0
        print(''.join([x[0] for x in res_list]))
    except:
        break
全部评论

相关推荐

点赞 2 评论
分享
牛客网
牛客企业服务