## 题目描述：

LeetCode 423. Reconstruct Original Digits from English

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"```

## 题目大意：

1. 输入只包含小写英文字母
2. 输入确保是有效的，并且一定可以转换为其原始数字。这意味着不会出现"abc", "zerone"之类的非法输入
3. 输入长度小于50000

## 解题思路：

```统计字符串s中各字符的个数，需要注意的是，在枚举英文字母时，需要按照特定的顺序方可得到正确答案。

## Python代码：

``````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 c in cntn:
cnts[c] -= t * cntn[c]
return ''.join(str(i) * n for i, n in enumerate(ans))
``````

Pingbacks已关闭。