题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
# 最小频数不止一个 while True: try: s = input() dic_count = {} for item in s: dic_count[item] = s.count(item) # 找出最少的频数: dic_value = list(dic_count.values()) dic_key = list(dic_count.keys()) min_count = min(dic_value) # 找出最少频数对应的key的位置: pos = [] for i in range(len(dic_count)): if dic_value[i] == min_count: pos.append(i) # 删除 for i in pos: s = s.replace(dic_key[i], '') print(s) except: break # # 上述代码可以进一步精简: # min_count = min(dic.values()) # for item in s: # if dic_count[item] == min_count: # s = s.replace(item, '') # print(s)