-
Notifications
You must be signed in to change notification settings - Fork 43
/
reconstruct-original-digits-from-english.py
82 lines (72 loc) · 2.4 KB
/
reconstruct-original-digits-from-english.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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
# V1 : DEV
# V2
# https://blog.csdn.net/fuxuemingzhu/article/details/79542812
import collections
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
# V3
# http://bookshadow.com/weblog/2016/10/16/leetcode-reconstruct-original-digits-from-english/
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))
# V4
# Time: O(n)
# Space: O(1)
from collections import Counter
class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
# The count of each char in each number string.
cnts = [Counter(_) for _ in ["zero", "one", "two", "three", \
"four", "five", "six", "seven", \
"eight", "nine"]]
# The order for greedy method.
order = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
# The unique char in the order.
unique_chars = ['z', 'o', 'w', 't', 'u', \
'f', 'x', 's', 'g', 'n']
cnt = Counter(list(s))
res = []
for i in order:
while cnt[unique_chars[i]] > 0:
cnt -= cnts[i]
res.append(i)
res.sort()
return "".join(map(str, res))