423. Reconstruct Original Digits from English - cocoder39/coco39_LC GitHub Wiki

423. Reconstruct Original Digits from English

  1. find out the rule: eg some char is only used by single digit
  2. how to implement in well-structured code
class Solution:
    def originalDigits(self, s: str) -> str:
        counter = collections.Counter(s)
        
        freq = [0] * 10
        freq[0] = counter['z']
        freq[2] = counter['w']
        freq[4] = counter['u']
        freq[6] = counter['x']
        freq[8] = counter['g']
        freq[3] = counter['h'] - freq[8]
        freq[5] = counter['f'] - freq[4]
        freq[7] = counter['s'] - freq[6]
        freq[9] = counter['i'] - freq[5] - freq[6] - freq[8]
        freq[1] = counter['n'] - freq[7] - 2 * freq[9]
        
        res = []
        for i in range(10):
            for j in range(freq[i]):
                res.append(str(i))
        return ''.join(res)