-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReconstructOriginalDigitsFrmEnglish.py
More file actions
70 lines (49 loc) · 2.2 KB
/
ReconstructOriginalDigitsFrmEnglish.py
File metadata and controls
70 lines (49 loc) · 2.2 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
from collections import Counter
found = True
lst = ["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]
for char in s:
if char not in lst:
found = False
if (found == False):
raise ValueError("Input string has invalid characters.")
if not (1 <= len(s) <= 100000):
raise ValueError("Input out of bounds")
op = ''
counter_dict = Counter(s)
stringNum_to_num = self.init_stringNum_to_num()
while (max(counter_dict.values()) != 0):
for elem in stringNum_to_num: # move in ascending order
found = True
for char in elem: # order matters # this will just get only one occurance
if (char in s) and (found):
continue #found this char; go to next
else:
found = False
if (found == True): #at least one
op = op + str(stringNum_to_num[elem])
for char in elem:
counter_dict[char] = counter_dict[char] - 1
print (len(op))
return (''.join(sorted(op)))
def init_stringNum_to_num(self):
from collections import OrderedDict
dict_stringNum_to_num = OrderedDict()
dict_stringNum_to_num["zero"] = "0"
dict_stringNum_to_num["one"] = "1"
dict_stringNum_to_num["two"] = "2"
dict_stringNum_to_num["three"] = "3"
dict_stringNum_to_num["four"] = "4"
dict_stringNum_to_num["five"] = "5"
dict_stringNum_to_num["six"] = "6"
dict_stringNum_to_num["seven"] = "7"
dict_stringNum_to_num["eight"] = "8"
dict_stringNum_to_num["nine"] = "9"
return dict_stringNum_to_num
out = Solution().originalDigits("zerozerooneonethreetheretwo")
print (out)