题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#参考:https://blog.nowcoder.net/n/f00d268a19824d6b868a76f261ee99ff?f=comment while True: try: s = input() dic, res = {}, '' for c in s: if c not in dic: dic[c] = 1 else: dic[c] += 1 Min = min(dic.values()) for c in s: if dic[c] != Min: res += c print(res) except: break ################################################################################ #####下面是我自己写的,不知道为什么用例通过19/20,assssa下面的代码输出的是assssa##### ################################################################################ ''' while True: try: inputstring = input() if len(inputstring) <= 20: dict_char = {} # 用字典记录每个字符的出现次数 min_count = 20 #记录最小出现次数,因为最多20个字符,所以初始化20次 for char in inputstring: if char in dict_char: dict_char[char] += 1 #char对应的值+1 else: dict_char[char] = 1 #记录新出现的字符 min_count = min(min_count, dict_char[char]) newlist = [] for char in inputstring: if dict_char[char] > min_count: # 只保留出现次数大于最小次数的字符 newlist.append(char) for element in newlist: print(element, end='') else: break except: break '''