LeetCode 423 Reconstruct Original Digits from English (Python)

Posted by 小明MaxMing on March 28, 2021

题目

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

Note:

  1. Input contains only lowercase English letters.
  2. Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as “abc” or “zerone” are not permitted.
  3. Input length is less than 50,000.

解题思路

将字符串存入计数器,找到每个单词的特殊字母求出个数

代码

class Solution:
    def originalDigits(self, s: str) -> str:
        ct = Counter(s)
        res = [
            ct["z"],
            ct["o"] - ct["z"] - ct["w"] - ct["u"],
            ct["w"],
            ct["t"] - ct["w"] - ct["g"],
            ct["u"],
            ct["f"] - ct["u"],
            ct["x"],
            ct["s"] - ct["x"],
            ct["g"],
            ct["i"] - ct["x"] - ct["g"] - ct["f"] + ct["u"],
        ]
        return "".join(str(d) * c for d, c in enumerate(res))

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅