Python Easy Solution


  • 0
    D
    from collections import Counter
    class Solution(object):
        def originalDigits(self, s):
            """
            :type s: str
            :rtype: str
            """
            digits = ['zero','one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
            digit_counts = [Counter(x) for x in digits]
            
            count = Counter(s)
            words = [0]*10
            
            while 'z' in count:
                words[0]+=1
                count -= digit_counts[0]
            # ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
            while 'w' in count:
                words[2]+=1
                count -= digit_counts[2]
            # ['one',  'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
            while 'u' in count:
                words[4]+=1
                count -= digit_counts[4]
            # ['one',  'three', 'five', 'six', 'seven', 'eight', 'nine']
            while 'f' in count:
                words[5]+=1
                count -= digit_counts[5]
            # ['one',  'three',  'six', 'seven', 'eight', 'nine']
            while 'x' in count:
                words[6]+=1
                count -= digit_counts[6]
            # ['one',  'three',  'seven', 'eight', 'nine']
            while 'v' in count:
                words[7]+=1
                count -= digit_counts[7]
            # ['one',  'three', 'eight', 'nine']
            while 'r' in count:
                words[3]+=1
                count -= digit_counts[3]
            # ['one', 'eight', 'nine']
            while 't' in count:
                words[8]+=1
                count -= digit_counts[8]
            # ['one', 'nine']
            while 'o' in count:
                words[1]+=1
                count -= digit_counts[1]
            # ['nine']
            words[9]+= count.get('i', 0)
            return ''.join( [str(x)*words[x] for x in range(10)] )

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.