【LeetCode】【字符串】题号:*423. 从英文中重建数字

every blog every motto: You will never know unless you try

0. 前言

生活好难,再坚持坚持!

1. 字符串

在这里插入图片描述

1.1 题目

在这里插入图片描述

1.2

class Solution:
    def originalDigits(self, s: str) -> str:
        # 统计各字符出现的数量
        count = collections.Counter(s)
        # print(count)
        out = {
    
    }  # 统计各数字出现的次数
        out['0'] = count['z']  # 0 zero
        out['2'] = count['w']  # 2 two
        out['4'] = count['u']  # 4 four
        out['6'] = count['x']  # 6 six
        out['8'] = count['g']  # 8 eight
        out['3'] = count['h'] - out['8']  # 3 three = h - eight
        out['5'] = count['f'] - out['4']  # 5 five = f - four
        out['7'] = count['s'] - out['6']  # 7 severn = s - six
        out['9'] = count['i'] - out['5'] - out['6'] - out['8']
        out['1'] = count['n'] - out['7'] - 2 * out['9']

        output = [key * out[key] for key in sorted(out.keys())]

        result = ''.join(output)

        return result

1.3

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39190382/article/details/119335243