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

思路:

1.遍历字符串,用字典记录每个字符出现的次数

2. min()找出最小的字符对应的次数及字符

3. 输出除待删除以外的其他字符

s=input().strip()
countdict={}
for c in s:
    countdict[c]=countdict.get(c,0)+1
mincount=min(countdict.values())
delchar=[]
goallist=[]
for k,v in countdict.items():
    if v==mincount:
        delchar.append(k)
for c in s:
    if c not in delchar:
        goallist.append(c)
goal=''.join(goallist)
print(goal)



全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务