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/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值