requirements
Given a non-empty string that contains the numbers 0-9 represented by an alphabetically scrambled English word. Output the raw number in ascending order.
Note:
The input contains only lowercase letters. The input is guaranteed to be legal and can be converted to raw numbers, which means that inputs like “ABC” or “zerone” are not allowed. The length of the input string is less than 50,000. Example 1:
Input: "owoztneoer" Output: "012" (Zeroonetwo)Copy the code
Example 2:
Input: "fviefuro" Output: "45" (fourfive)Copy the code
The core code
class Solution:
def originalDigits(self, s: str) - >str:
import collections
order = ["zero"."two"."four"."six"."one"."three"."five"."seven"."eight"."nine"]
find = ["z"."w"."u"."x"."o"."r"."f"."v"."t"."e"]
digit = [0.2.4.6.1.3.5.7.8.9]
record = [0 for _ in range(10)]
dic = collections.Counter(s)
for idx in range(10):
cnt = dic[find[idx]]
record[digit[idx]] += cnt
dic = dic - collections.Counter(order[idx] * cnt)
if not dic:
break
ress = ""
for i in range(10):
ress += str(i) * record[i]
return ress
Copy the code
Answer: We can identify certain numbers that have absolute features, such as zero’s z, zero’s W, two’s W, and so on. We can kick the numbers that can be determined out of the statistical dictionary and, bit by bit, determine who the rest of the numbers come from. Finally, the statistical figures can be spliced.