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

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

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

#字符串处理函数
def processStr(line):
    ch_nums={}
    #统计字符频率
    for index in range(len(line)):
        curch = line[index]
        nums = ch_nums.get(curch)
        if nums:
            ch_nums.__setitem__(curch, nums+1)
        else:
            ch_nums.__setitem__(curch, 1)
            
    values = ch_nums.values()
    minValues = min(values)
    #根据最小频率,确定需要删除的字符
    delChar = []
    for key in ch_nums.keys():
        if ch_nums.get(key)==minValues:
            delChar.append(key)
            
    #输出删除后的字符串
    res = ''
    for i in range(len(line)):
        if line[i] not in delChar:
            res+=line[i]
    return res
    
while 1:
    try:
        line = input().strip()
        print(processStr(line))
    except EOFError:
        break
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 11:27
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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