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

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

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

import sys
'''
step1: 计算字符及其频数:dic = {'a':2,'b':1,...}
step2: 删除原字符串频数最低的字符(一种或多种字符):re.sub(*) 或 string.replace(*)
other: 统一为小写:string.lower()
'''
while True:
    try:
        v_str = input().lower()
        cnt_dic = dict.fromkeys(set(v_str),0)
        for i_char in cnt_dic.keys():
            cnt_dic[i_char] = v_str.count(i_char)
        min_val = min(cnt_dic.values())
        for (i_key, i_val) in cnt_dic.items():
            if i_val == min_val:
                v_str = v_str.replace(str(i_key), '')
        print(v_str)
    except:
        break

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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