题解 | #删除字符串中出现次数最少的字符#

'''
写复杂了,本质上是用set去重,再用字典记录每个字符的数量,排序后去掉最小的字符,有很多地方还可以优化
'''
x = input()
x_list = list(x)
x_set = set(x_list)
x_dict = {}
for i in x_set:
    x_dict[i] = x.count(i)

x_key = dict(sorted(x_dict.items(),key=lambda x:x[1],reverse = False)).keys()
ans_list = []
for key in x_key:
    if not ans_list:
        ans_list.append(key)
    elif x_dict[key]==x_dict[ans_list[-1]]:
        ans_list.append(key)
    elif x_dict[key] > x_dict[ans_list[-1]]:
        break
    else:
        break
for key in ans_list:
    x = x.replace(key,'')
print(x)

全部评论

相关推荐

牛大宝儿236:还没入职就PUA,[发火我之前遇到一个月给500块钱的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务