423、从英文中重建数字

给你一个字符串 s ,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9)。按 升序 返回原始的数字。

示例 1:
输入:s = “owoztneoer”
输出:“012”

示例 2:
输入:s = “fviefuro”
输出:“45”

提示:
1 <= s.length <= 105
s[i] 为 [“e”,“g”,“f”,“i”,“h”,“o”,“n”,“s”,“r”,“u”,“t”,“w”,“v”,“x”,“z”] 这些字符之一
s 保证是一个符合题目要求的字符串

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reconstruct-original-digits-from-english
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# 根据不同字母出现频率判断某个数字出现的次数
"""
z 只出现在 0 中
w 只出现在 2 中
u 只出现在 4 中
x 只出现在 6 中
g 只出现在 8 中
h 只出现在 3、8 中
f 只出现在 4、5 中
s 只出现在 6、7 中
o 只出现在 0、1、2、4 中
i 只出现在 5、6、8、9 中
"""
class Solution:
    def originalDigits(self, s: str) -> str:
    	# Counter可将字符串中出现的字符及其出现次数转换为键值对
        c = collections.Counter(s)

        cnt = [0] * 10

        cnt[0] = c["z"]
        cnt[2] = c["w"]
        cnt[4] = c["u"]
        cnt[6] = c["x"]
        cnt[8] = c["g"]

        cnt[3] = c["h"] - cnt[8]
        cnt[5] = c["f"] - cnt[4]
        cnt[7] = c["s"] - cnt[6]

        cnt[1] = c["o"] - cnt[0] - cnt[2] - cnt[4]
        cnt[9] = c["i"] - cnt[5] - cnt[6] - cnt[8]

        res = ""
        
        for i in range(10):
            res += str(i) * int(cnt[i])

        return res


# 报错,遍历次数过多
class Solution:
    def originalDigits(self, s: str) -> str:
        res = {0: "", 1: "", 2: "", 3: "", 4: "", 5: "", 6: "", 7: "", 8: "", 9: ""}

        def cir(self, s: str):
            if s == "":
                return

            for i in s:
                if i == "z" and s.count("e") > 0 and s.count("r") > 0 and s.count("o") > 0:
                    res[0] += "0"
                    ns = s.replace("z", "", 1).replace("e", "", 1).replace("r", "", 1).replace("o", "", 1)
                    cir(self, ns)
                    return

                if i == "o" and s.count("n") > 0 and s.count("e") > 0:
                    res[1] += "1"
                    ns = s.replace("o", "", 1).replace("n", "", 1).replace("e", "", 1)
    
                    cir(self, ns)
                    return

                if i == "t" and s.count("w") > 0 and s.count("o") > 0:
                    res[2] += "2"
                    ns = s.replace("t", "", 1).replace("w", "", 1).replace("o", "", 1)
                    cir(self, ns)
                    return

                if i == "t" and s.count("h") > 0 and s.count("r") > 0 and s.count("e") > 1:
                    res[3] += "3"
                    ns = s.replace("t", "", 1).replace("h", "", 1).replace("r", "", 1).replace("e", "", 2)
                    cir(self, ns)
                    return

    
                if i == "f" and s.count("o") > 0 and s.count("u") > 0 and s.count("r") > 0:
                    res[4] += "4"
                    ns = s.replace("f", "", 1).replace("o", "", 1).replace("u", "", 1).replace("r", "", 1)
                    cir(self, ns)
                    return

                if i == "f" and s.count("i") > 0 and s.count("v") > 0 and s.count("e") > 0:
                    res[5] += "5"
                    ns = s.replace("f", "", 1).replace("i", "", 1).replace("v", "", 1).replace("e", "", 1)
                    cir(self, ns)
                    return

                if i == "s" and s.count("i") > 0 and s.count("x") > 0:
                    res[6] += "6"
                    ns = s.replace("s", "", 1).replace("i", "", 1).replace("x", "", 1)
                    cir(self, ns)
                    return

                if i == "s" and s.count("e") > 1 and s.count("v") > 0 and s.count("n") > 0:
                    res[7] += "7"
                    ns = s.replace("s", "", 1).replace("e", "", 2).replace("v", "", 1).replace("n", "", 1)
                    cir(self, ns)
                    return

                if i == "e" and s.count("i") > 0 and s.count("g") > 0 and s.count("h") > 0 and s.count("t") > 0:
                    res[8] += "8"
                    ns = s.replace("e", "", 1).replace("i", "", 1).replace("g", "", 1).replace("h", "", 1).replace("t", "", 1)
                    cir(self, ns)
                    return

                if i == "n" and s.count("n") > 1 and s.count("i") > 0 and s.count("e") > 0:
                    res[9] += "9"
                    ns = s.replace("n", "", 2).replace("i", "", 1).replace("e", "", 1)
                    cir(self, ns)
                    return

        cir(self, s)

        fin = ""

        for i in res.values():
            fin += i

        return fin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值