题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import sys
inp = input()
# print(len(inp))
dic = dict() #字典统计出现的字母次数,这样可以不用讨论用hash表时那些出现次数为0的字母
for word in inp:
if word in dic.keys():
dic[word] +=1
else:
dic[word] = 1
min_nums = min(dic.values()) #找出最小的次数
a = []
for key,value in dic.items(): #遍历,用个新列表保存
if value==min_nums:
a.append(key)
ans = []
for w in inp:
if w not in a:
ans.append(w)
print(''.join(ans)) #输出,虽然是笨方法,但是好像代码才20行
查看23道真题和解析