题解 | #HJ23 删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
Python版本
content = input()
index_map = {chr(i):[] for i in range(ord('a'), ord('z')+1)}
for i, c in enumerate(content):
index_map[c].append(i)
# 每种字符的个数
index_counts = [(c, len(index_map[c])) for c in index_map if index_map[c] != []]
index_counts.sort(key=lambda x: x[1])
# print(index_counts)
count_min = min([x[1] for x in index_counts])
# print(count_min)
count_min_num = [x[1] for x in index_counts].count(count_min)
# print(count_min_num)
for j in range(count_min_num):
index_counts.pop(0)
# print(index_counts)
remain_c = [x[0] for x in index_counts]
# print(remain_c)
remain_index_map = {c: index_map[c] for c in remain_c}
# print(remain_index_map)
index_c = [''] * 21
for c in remain_index_map:
for i in remain_index_map[c]:
index_c[i] = c
result = ''.join(index_c)
print(result)