class Solution:
def originalDigits(self, s: str) -> str:
"""
zero z
one o 1-0-2-4
two w
three t 3-2-8
four u
five f 5-4
six x
seven s 7-6
eight g
nine i 9-8-6-5
"""
# step1 c
cnt = [0] * 10
# first even then odd
for c in s:
if c == 'z':
cnt[0] += 1
elif c == 'w':
cnt[2] += 1
elif c == 'u':
cnt[4] += 1
elif c == 'x':
cnt[6] += 1
elif c == 'g':
cnt[8] += 1
elif c == 'o':
cnt[1] += 1
elif c == 't':
cnt[3] += 1
elif c == 'f':
cnt[5] += 1
elif c == 's':
cnt[7] += 1
elif c == 'i':
cnt[9] += 1
# step2 process odd numbser
cnt[5] = cnt[5]-cnt[4]
cnt[7] = cnt[7]-cnt[6]
cnt[1] = cnt[1]-cnt[0]-cnt[2]-cnt[4]
cnt[9] = cnt[9]-cnt[8]-cnt[6]-cnt[5]
cnt[3] = cnt[3]-cnt[8]-cnt[2]
# step3, join string together
res = []
for num in range(len(cnt)):
if cnt[num] != 0:
res.append(str(num)* cnt[num])
return ''.join(res)
optimization: use counter to record all char cnt in one round and substract based on dependency ; also use string as key and sort before append to final result