笨办法也没有特别好的思路,遍历每个位置上字母出现的次数
import sys
lines = sys.stdin
for data in lines:
data = [x for x in data.strip()]
str_length = len(data)
letter_count = [0 for x in range(str_length)]
for i in range(str_length):
for j in range(str_length):
if data[j] == data[i]:
letter_count[i] += 1
min_count = min(letter_count)
new_data = ''
for i in range(str_length):
if letter_count[i] != min_count:
new_data += data[i]
print(new_data)
另一个方法,遍历所有的字母,然后将其出现的数量标注在string.ascii_letter同步的数组上,如果是其对应字母总数的位置上为最小值,则替换所有字母为'',最后即可达到合理字符串
import sys
from string import ascii_lowercase
lines = sys.stdin
ascii_lowercase = [case for case in ascii_lowercase]
for data in lines:
case_count = [0 for x in ascii_lowercase]
data = data.strip()
for case in data:
index = ascii_lowercase.index(case)
case_count[index] += 1
min_count = min(filter(None, case_count))
for i in range(len(case_count)):
if case_count[i] == min_count:
data = data.replace(ascii_lowercase[i], '')
print(data)