-
Notifications
You must be signed in to change notification settings - Fork 0
/
423ReconstructOriginalDigitsFromEnglish.py
42 lines (37 loc) · 1.3 KB
/
423ReconstructOriginalDigitsFromEnglish.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.
Note:
Input contains only lowercase English letters.
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.
Input length is less than 50,000.
Example 1:
Input: "owoztneoer"
Output: "012"
Example 2:
Input: "fviefuro"
Output: "45"""
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
"""
zero: Only digit with z
two: Only digit with w
four: Only digit with u
six: Only digit with x
eight: Only digit with g
The odd ones for easy looking, each one's letters all also appear in other digit words:
one, three, five, seven, nine
"""
res = ""
res += "0"*s.count('z')
res += "1"*(s.count('o')-s.count('z')-s.count('w')-s.count('u'))
res += "2"*s.count('w')
res += "3"*(s.count('h') - s.count('g'))
res += "4"*s.count('u')
res += "5"*(s.count('f') - s.count('u'))
res += "6"*s.count('x')
res += "7"*(s.count('s')-s.count('x'))
res += "8"*s.count("g")
res += "9"*(s.count('i') - s.count('x') - s.count("g") - s.count('f') + s.count('u'))
return res