Leetcode 423 Solution

This article provides solution to leetcode question 423 (reconstruct-original-digits-from-english)

https://leetcode.com/problems/reconstruct-original-digits-from-english

Solution

class Solution {
public:
    string originalDigits(string s) {
        vector<int> a(26);

        for (auto it = s.begin(); it != s.end(); it++)
        {
            if (*it == 'z')
                a[0]++;
            else if (*it == 'x')
                a[6]++;
            else if (*it == 'w')
                a[2]++;
            else if (*it == 'u')
                a[4]++;
            else if (*it == 'g')
                a[8]++;
            else if (*it == 's') // 7 and 6
                a[7]++;
            else if (*it == 'o') // 0, 1, 2, 4
                a[1]++;
            else if (*it == 'h') // 3 and 8
                a[3]++;
            else if (*it == 'v') // 5 and 7
                a[5]++;
            else if (*it == 'i') // 5, 6, 8, 9
                a[9]++;
        }

        a[7] -= a[6];
        a[1] -= a[0] + a[2] + a[4];
        a[3] -= a[8];
        a[5] -= a[7];
        a[9] -= a[5] + a[6] + a[8];

        string res;

        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < a[i]; j++)
                res += to_string(i);
        }

        return res;
    }
};