# 【LeetCode】423. Reconstruct Original Digits from English 解题报告（Python）

# 题目描述：

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.

Example 1: Input: "owoztneoer"

Output: "012"

Example 2: Input: "fviefuro"

Output: "45"

# 解题方法

``````class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
number = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
count = collections.Counter(s)
res = ''
for i, num in enumerate(number):
while True:
word_count = 0
for c in num:
if count[c] > 0:
word_count += 1
if word_count == len(num):
res += str(i)
count.subtract(collections.Counter(num))
else:
break
return res
``````

``````class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
cnts = collections.Counter(s)
nums = ['six', 'zero', 'two', 'eight', 'seven', 'four', 'five', 'nine', 'one', 'three']
numc = [collections.Counter(num) for num in nums]
digits = [6, 0, 2, 8, 7, 4, 5, 9, 1, 3]
ans = [0] * 10
for idx, num in enumerate(nums):
cntn = numc[idx]
t = min(cnts[c] /  cntn[c] for c in cntn)
ans[digits[idx]] = t
for i in range(t):
cnts.subtract(cntn)
return ''.join(str(i) * n for i, n in enumerate(ans))
``````

2018 年 3 月 13 日