美文网首页Leetcode
Leetcode 423. Reconstruct Origin

Leetcode 423. Reconstruct Origin

作者: SnailTyan | 来源:发表于2021-08-02 11:20 被阅读0次

    文章作者: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 Origin

        本文链接:https://www.haomeiwen.com/subject/acoypltx.html