题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
n = input() # 假设用户输入的字符串存储在变量n中 l = {char:n.count(char) for char in n} lr = [char for char in n] min_value = min(l.values()) key_to_remove = [key for key,value in l.items() if value == min_value] index_list = [] for char in key_to_remove: for i,v in enumerate(n): if v == char: index_list.append(i) print(''.join([lr[i] for i in range(len(lr)) if i not in index_list]))
定义一个字典来记录每个字符出现次数,定义一个列表存储字符,计算字典中value最小的值min_value,判断生成需要去掉的key,
将需要去掉的字符和储存列表进行匹配,记录索引,除去这些去掉的索引就是输出