Leetcode 423. Reconstruct Original Digits from English

2021-07-15 14:06:02 浏览数 (1)

1. Description

2. Solution

**解析:**Version 1,找出0-9英文字母个数与数字个数的对应关系,然后根据统计的字符数计算对应的数字个数,按顺序构造最后的字符串即可。

  • Version 1
代码语言:javascript复制
class Solution:
    def originalDigits(self, s: str) -> str:        
        chars = ["e", "g", "f", "i", "h", "o", "n", "s", "r", "u", "t", "w", "v", "x", "z"]
        stat = {ch: 0 for ch in chars}
        for ch in s:
            stat[ch]  = 1

        result = ''
        result  = '0' * stat['z']
        result  = '1' * (stat['o'] - stat['z'] - stat['w'] - stat['u'])
        result  = '2' * stat['w']
        result  = '3' * (stat['r'] - stat['z'] - stat['u'])
        result  = '4' * stat['u']
        result  = '5' * (stat['f'] - stat['u'])
        result  = '6' * stat['x']
        result  = '7' * (stat['v'] - (stat['f'] - stat['u']))
        result  = '8' * stat['g']
        result  = '9' * (stat['i'] - (stat['f'] - stat['u']) - stat['x'] - stat['g'])
        return result

Reference

  1. https://leetcode.com/problems/reconstruct-original-digits-from-english/

0 人点赞