Leetcode 423. Reconstruct Original Digits from English

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 423 Reconstruct Original Digits from English

[LeetCode]423. Reconstruct Original Digits from English

LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路

Leetcode 423. Reconstruct Original Digits from English

Leetcode-423 Reconstruct Original Digits from English(从英文中重建数字)

423. Reconstruct Original Digits from English(Medium)