题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
# 删除字符串出现次数最少的字符 input_str = input() def letter_idea(raw_str): """方法1,利用全部字母遍历(在输入不出现英文字母以外的情况下有效)""" letters = [item for item in 'abcdefghijklmnopqrstuvwxyz'] detail = dict() for letter in letters: num = raw_str.count(letter) if num == 0: pass elif num in detail.keys(): detail[num].append(letter) else: detail[num] = [letter] # 所有字符出现次数全部相等,则打印原始字符串 if len(detail) == 1: print(raw_str) else: min_num = min(detail.keys()) for i in detail[min_num]: raw_str = raw_str.replace(i, '') print(raw_str) def general_idea(raw_str): """方法2,通用方法""" detail, res = dict(), str() # 数据收集 for s in raw_str: if s not in detail: detail[s] = 1 else: detail[s] += 1 min_num = min(detail.values()) # 结果处理 if len(set(detail.values())) == 1: print(raw_str) else: for s in raw_str: if detail[s] != min_num: res += s # print(detail) print(res) if __name__ == '__main__': letter_idea(input_str) general_idea(input_str)