LeetCode刷题实战423:从英文中重建数字
Given a string s containing an out-of-order English representation of digits 0-9, return the digits in ascending order.
示例
示例 1:
输入: "owoztneoer"
输出: "012" (zeroonetwo)
示例 2:
输入: "fviefuro"
输出: "45" (fourfive)
解题
class Solution:
def DeterNums(self, index, Alphabet, numsE):
#check
if( not all([ True if Alphabet[w] >0 else False for w in numsE[1] ])):
return;
E = numsE[1][-1]
if numsE[0] == 9:
index[numsE[0]] = Alphabet[E]//2
Alphabet['n'] = index[numsE[0]]
else:
index[numsE[0]] = Alphabet[E]
for v in numsE[1]:
Alphabet[v] -= index[numsE[0]]
def originalDigits(self, s: str) -> str:
Alphabet = collections.Counter(s)
index, re = [0] * 10, ''
numsE = [[2, 'tow'], [0, 'eroz'], [6, 'six'], [4, 'foru'],
[7, 'evens'], [1, 'neo'], [5, 'ivef'], [9, 'ien'],
[8, 'eghti'], [3, 'treeh']]
for nE in numsE:
self.DeterNums(index, Alphabet, nE)
re = ''
for i in range(10):
re += str(i)*index[i]
return re