实现删除字符串中出现次数最少的字符,若出现次数最少的字符有多个,则把出现次数最少的字符都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。
数据范围:输入的字符串长度满足 ,保证输入的字符串中仅出现小写字母
字符串只包含小写英文字母, 不考虑非法输入,输入的字符串长度小于等于20个字节。
删除字符串中出现次数最少的字符后的字符串。
aabcddd
aaddd
from collections import Counter input_str = input() c = Counter(input_str) kv = c.most_common() kv.sort(key=lambda v: v[1]) min_count = kv[0][-1] for item in kv: if item[-1] == min_count: input_str = input_str.replace(item[0], '') else: break print(input_str)
import sys dic = {} count_list = [] string = '' result_str = '' for line in sys.stdin: a = line.split() for s in a[0]: if s in dic: dic[s] = dic[s] + 1 else: dic[s] = 1 # ========================== for item in dic.values(): count_list.append(item) # 可简化! min_value = min(count_list) #====================================== if all(item == min_value for item in dic.values()): print(a[0]) else: # 可简化,没必要写两个 for for keys in dic.keys(): if dic[keys] == min_value: string = string + keys # 可简化! for s in a[0]: if s not in string: result_str = result_str + s print(result_str,end='\n')
string = list(input()) test1,test2 = '','' for alp in string: if alp not in test1: test1+= alp mark = string.count(string[0]) for alp in test1: if mark >= string.count(alp): mark = string.count(alp) for alp in string: if string.count(alp) != mark: test2 += alp print(test2)
# 初步思路,使用字典进行设置 list1 = input() temp = '' # 用于保存中间变量 dict1 = {} for i in list1: if i in temp: # 假如出现重复的元素之后,那么对它的value值+1 dict1[i] = dict1[i] + 1 else: dict1[i] = 1 # 如果第一次出现,那么它的value值就是1 temp = temp + i # 把遍历出来的字符保存在变量中,方便后面判断重复元素 # 找到字典里面最小的value值 num = min(dict1.values()) # 获取这些最小的的key值对应的value的值--无法这样操作,所以使用遍历字典进行操作 k = '' new_str = list(list1) for j in dict1: if dict1[j] == num: for m in range(dict1[j]): new_str.remove(j) print("".join(new_str))
while True: try: raw_input = input() alpha_set = list(set(raw_input)) min_num = 30 mid_lst = [] for num, i in enumerate(alpha_set): if raw_input.count(i)<min_num: min_num = raw_input.count(i) mid_lst.clear() mid_lst.append(i) elif raw_input.count(i) == min_num: mid_lst.append(i) if num == len(alpha_set)-1: for j in mid_lst: raw_input = raw_input.replace(j, '') print(raw_input) except: break
import sys for line in sys.stdin: a = line.split() str_input = a[0] dict_str = {} for i in str_input: if(i not in dict_str.keys()): dict_str[i] = 1 else: dict_str[i] = dict_str[i] + 1 result = sorted(dict_str.items(),key = lambda s:s[1]) min_number = result[0][1] finally_str = [] for i in result: if(i[1] == min_number): finally_str.append(i[0]) result_str = str_input[:] i = 0 while(i != len(str_input)): if(str_input[i] in finally_str): str_input = str_input[:i] + str_input[i+1:] i = i else: i = i+1 continue print(str_input)