Skip to content

Commit 581b9f1

Browse files
committed
2019-11-30 423. Reconstruct Original Digits from English Solution
1 parent 7529d69 commit 581b9f1

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# LeetCode 423. Reconstruct Original Digits from English
2+
3+
## Description
4+
5+
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.
6+
7+
Note:
8+
9+
Input contains only lowercase English letters.
10+
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.
11+
Input length is less than 50,000.
12+
13+
Example 1:
14+
```py
15+
Input: "owoztneoer"
16+
17+
Output: "012"
18+
```
19+
20+
Example 2:
21+
```py
22+
Input: "fviefuro"
23+
24+
Output: "45"
25+
```
26+
## 描述
27+
28+
给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9。按升序输出原始的数字。
29+
30+
注意:
31+
32+
输入只包含小写英文字母。
33+
输入保证合法并可以转换为原始的数字,这意味着像 "abc" 或 "zerone" 的输入是不允许的。
34+
输入字符串的长度小于 50,000。
35+
示例 1:
36+
37+
```py
38+
输入: "owoztneoer"
39+
40+
输出: "012" (zeroonetwo)
41+
```
42+
43+
示例 2:
44+
```py
45+
输入: "fviefuro"
46+
47+
输出: "45" (fourfiv
48+
```
49+
来源:力扣(LeetCode)
50+
链接:https://leetcode-cn.com/problems/reconstruct-original-digits-from-english
51+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
52+
53+
### 思路
54+
55+
* 这道题考数学,考观察,没有考察到什么算法。
56+
* 'z' 只出现在 zero 中,因此 z 唯一确定 0 的个数;
57+
* 同理,w,u,x,g 分别只出现在 two,four,six,eight 中,因此唯一确定 2468.
58+
* o 出现在 zero,two,four,one 中,由于 024 已经被确定,因此 1 可以被确定;
59+
* h 出现在 three ,eight 中,8 已经被确定,因此 3 可以被确定;
60+
* s 出现在 six,seven 中,由于 6 已经被确定,因此 7 可以被确定;
61+
* i 出现在 five,six,eight,nine 中,由于 568 已经被确定,因此 9 可以被确定。
62+
63+
```py
64+
# -*- coding: utf-8 -*-
65+
# @Author: 何睿
66+
# @Create Date: 2019-11-30 13:15:30
67+
# @Last Modified by: 何睿
68+
# @Last Modified time: 2019-11-30 13:28:08
69+
70+
from collections import Counter
71+
72+
73+
class Solution:
74+
def originalDigits(self, s: str) -> str:
75+
res = {}
76+
count = Counter(s)
77+
for key, char in zip([0, 2, 4, 6, 8], ['z', 'w', 'u', 'x', 'g']):
78+
res[key] = count.get(char, 0)
79+
res[1] = count.get('o', 0) - res[0] - res[2] - res[4]
80+
res[3] = count.get('h', 0) - res[8]
81+
res[5] = count.get("f", 0) - res[4]
82+
res[7] = count.get('s', 0) - res[6]
83+
res[9] = count.get('i', 0) - res[5] - res[6] - res[8]
84+
85+
return ''.join(str(num) * res[num] for num in range(0, 10))
86+
```
87+
88+
源代码文件在 [这里](https://github.com/ruicore/Algorithm/blob/master/LeetCode/2019-11-30-423-Reconstruct-Original-Digits-from-English.py) 。
89+
©本文首发于 何睿的博客 ,欢迎转载,转载需保留 [文章来源](https://ruicore.cn/leetcode-423-reconstruct-original-digits-from-english/) ,作者信息和本声明.

0 commit comments

Comments
 (0)