题解 | #RY机试之一#
实现删除字符串中出现次数最少的单词,若多个单词出现次数一样,则都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。(模糊记忆,不喜勿喷,谢谢!只记得具体的输入输出)
输入描述:
字符串的输入是一段英文单词字符串,不考虑非法输入,输入的字符串长度小于等于20个字节
输出描述:
删除字符串中出现次数最少的字符后的字符串
示例1
输入
This is a surface a
输出
a a
示例2
输入
is is a
输出
is is
ACM实现
编程语言:python3
实现方法:char --> list and dict,利用字典统计元素出现的个数,利用list返回结果
import sys from collections import Counter def find(str): nums = [] for i in str: nums.append(i) # print(nums) dict1 = Counter(str) # print(dict1) list1 = Counter.most_common(dict1) min_val = list1[-1][-1] # print(min_val) if len(dict1)>1: for key, val in dict1.items(): if val == min_val: for i in nums: if key == i: nums.remove(key) else: nums = [] # print(nums) for i in nums: print(i, end=" ") if __name__ == "__main__": str = [i for i in input().split(" ")] # print(str) find(str)