在一行上输入一个长度为
,仅由小写字母构成的字符串
,代表待处理的字符串。
在一行上输出一个字符串,代表删除后的答案。保证这个字符串至少包含一个字符。
aabcddd
aaddd
在这个样例中,出现次数最少的字符为
和
,因此需要同时删除这两个字符。
import sys def get_substr(s): dct = {} # 出现的字符作为key,次数作为value for c in s: dct[c] = dct.get(c, 0) + 1 # 获取dct的所有value,并取最小值 min_val = min(dct.values()) #遍历字典,当val等于min_val时,从元素字符串中删除 i = 0 for key in dct: if dct[key] == min_val: i += 1 s = s.replace(key, '') return s for line in sys.stdin: print(get_substr(line.strip()))
def gs(str): a=list(set(str)) n=len(a) x=[] for i in range(n): x.append(str.count(a[i])) zxz=min(x) for i in range(n): if x[i]==zxz: str=str.replace(a[i],'') else: str=str print(str) while True: try: a=input() gs(a) except: break
while True: try: str = input() chars = {} order = [] for x in str: if x in chars: chars[x] += 1 else: chars[x] = 1 order.append(x) least = chars[min(chars, key=chars.get)] output = '' for x in str: if int(chars[x]) > least: output = ''.join([output, x]) print(output) except EOFError: break
while True: try: src_str = input().strip() per_str = "".join(set(src_str)) lis = [src_str.count(i) for i in per_str ] mini = min(lis) to_del = [per_str[j] for j in range(len(lis)) if lis[j] == mini] for k in to_del: new_str = src_str.replace(k, '') src_str = new_str print(new_str) except EOFError: break
while True: try: data = str(input()) count = {} for i in data: if i not in count: count[i] = 1 else: count[i] += 1 myItemsCounts = min(count.values()) outData = data for key, value in count.items(): if value == myItemsCounts: outData = outData.replace(key, "") print(outData) except: break
很多答案找最小个数都是跟1比较大小,这忽略了最小个数大于1的情况,不准确 while True: try: string = input() word_dict = {} for item in string: if item not in word_dict: word_dict[item] = 1 else: word_dict[item] += 1 num_list=[] for index in word_dict: num_list.append(word_dict[index]) min_num = min(num_list) s_list = list(string) for index in range(len(s_list)-1,-1,-1): if word_dict[s_list[index]]==min_num: del s_list[index] print(''.join(s_list)) except: break