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

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

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

python3方法好像有点笨,但是通过了

while True:
    try:
        lst = list(input().strip())    # 转为列表
        str_lst = []                   # 存储相同的字符
        nums_lst = []                  # 记录字符出现的次数

        for item in lst:               # 统计所有字符出现的次数
            if item not in str_lst:
                str_lst.append(item)
                nums_lst.append(1)
            else:
                nums_lst[str_lst.index(item)] += 1

        min_num = min(nums_lst)        # 获取最小次数
        for i in nums_lst:
            if i > min_num:
                str_lst[nums_lst.index(i)] = ' '       # 将出现次数大于最小次数的字符删除,以便后续的去交集操作
                nums_lst[nums_lst.index(i)] = ' '

        output = [i for i in lst if i not in str_lst]  # 原字符串与出现次数最少的字符串去交集
        output = [str(i) for i in output]              # 列表转为字符串
        output = "".join(output)
        print(str(output))
    except:
        break
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务