423. Reconstruct Original Digits from English

来源:互联网 发布:seo推荐 编辑:程序博客网 时间:2024/06/05 11:11
class Solution(object):
    def originalDigits(self, s):
        """
        :type s: str
        :rtype: str
        """
        ss=collections.Counter(s)
        tmp=['z','o','w','t','u','f','x','s','g','i']
        i=0
        ans=''
        while i<len(tmp):
            if i%2==0 and tmp[i] in ss:
                for j in range(ss[tmp[i]]):
                    ans+="%s"%i
            elif  i%2==1 and tmp[i] in ss:
                if tmp[i]=='o':
                    n=ss[tmp[i]]-ss[tmp[0]]-ss[tmp[2]]-ss[tmp[4]]
                elif tmp[i]=='t':
                    n=ss[tmp[i]]-ss[tmp[2]]-ss[tmp[8]]
                elif tmp[i]=='f':
                   n=ss[tmp[i]]-ss[tmp[4]]
                elif tmp[i]=='s':
                   n=ss[tmp[i]]-ss[tmp[6]]
                elif tmp[i]=='i':
                   n=ss[tmp[i]]-ss[tmp[6]]-ss[tmp[8]]-ss['f']+ss[tmp[4]]
                for j in range(n):
                    ans+="%s"%i
            i+=1

        return ans

0,2,4,6,8分别由z,w,u,x,g确定

1,3,5,7,9分别由o,t,f,s,i:

13579的个数算法分别为减去后边的个数:

1--》0,2,4

3--》2,8

5--》4

7--》6

9--》6,8,5

由于5不能直接确定,所以在算9的时候要通过f个数减去4的个数算



阅读全文
0 0