Leetcode 423. Reconstruct Original Digits from English

文章作者:Tyan
博客:noahsnail.com  |  CSDN  | 

1. Description

Reconstruct Original Digits from English

2. Solution

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

  • Version 1
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/

你可能感兴趣的:(Leetcode 423. Reconstruct Original Digits from English)