题解 | #删除字符串中出现次数最少的字符# 队列 + 哈希

删除字符串中出现次数最少的字符

https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

from collections import deque, defaultdict
import sys

for line in sys.stdin:
    s = line.strip()
    q = deque()
    counter = defaultdict(int)
    for c in s:
        q.append(c)
        counter[c] += 1
    del_set = set()
    mini_cnt = len(s)
    for key in counter.keys():
        mini_cnt = min(mini_cnt, counter[key])
    for key in counter.keys():
        if counter[key] == mini_cnt:
            del_set.add(key)
    ans = list()
    while q:
        c = q.popleft()
        if c not in del_set:
            ans.append(c)
    print(''.join(ans))

全部评论

相关推荐

牛客717484937号:双飞硕没实习挺要命的
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务