C++ Solution should be easy to understand


  • 0
    Y
    class Solution {
    public:
        string originalDigits(string s) {
            string ans = "", tmp = "";
            if(s.size() == 0)
                return ans;
            vector<int> lettercnt(26, 0), digitcnt(10, 0);
            for(int i = 0; i < s.size(); ++ i){
                ++ lettercnt[s[i] - 'a'];
            }
            for(int i = 0; i < 10; ++ i){
                getDigit(digitorder[i], lettercnt, digitcnt);
            }
            for(int i = 0; i < 10; ++ i){
                while(digitcnt[i] > 0){
                    -- digitcnt[i];
                    tmp = ('0'+i);
                    ans += tmp;
                }
            }
            return ans;
        }
    private:
        void getDigit(int digit, vector<int> &lettercnt, vector<int> &digitcnt){
            switch(digit){
                case 0:
                    while(lettercnt[25] > 0){
                        -- lettercnt[25];
                        -- lettercnt[4];
                        -- lettercnt[17];
                        -- lettercnt[14];
                        ++ digitcnt[digit];
                    }
                    break;
                case 1:
                    while(lettercnt[14] > 0){
                        -- lettercnt[13];
                        -- lettercnt[14];
                        -- lettercnt[4];
                        ++ digitcnt[digit];
                    }
                    break;
                case 2:
                    while(lettercnt[22] > 0){
                        -- lettercnt[22];
                        -- lettercnt[14];
                        -- lettercnt[19];
                        ++ digitcnt[digit];
                    }
                    break;
                case 3:
                    while(lettercnt[19] > 0){
                        -- lettercnt[19];
                        -- lettercnt[7];
                        -- lettercnt[17];
                        lettercnt[4] -= 2;
                        ++ digitcnt[digit];
                    }
                    break;
                case 4:
                    while(lettercnt[20] > 0){
                        -- lettercnt[5];
                        -- lettercnt[14];
                        -- lettercnt[20];
                        -- lettercnt[17];
                        ++ digitcnt[digit];
                    }
                    break;
                case 5:
                    while(lettercnt[5] > 0){
                        -- lettercnt[5];
                        -- lettercnt[8];
                        -- lettercnt[21];
                        -- lettercnt[4];
                        ++ digitcnt[digit];
                    }
                    break;
                case 6:
                    while(lettercnt[23] > 0){
                        -- lettercnt[23];
                        -- lettercnt[8];
                        -- lettercnt[18];
                        ++ digitcnt[digit];
                    }
                    break;
                case 7:
                    while(lettercnt[18] > 0){
                        -- lettercnt[18];
                        lettercnt[4] -= 2;
                        -- lettercnt[21];
                        -- lettercnt[13];
                        ++ digitcnt[digit];
                    }
                    break;
                case 8:
                    while(lettercnt[6] > 0){
                        -- lettercnt[4];
                        -- lettercnt[8];
                        -- lettercnt[6];
                        -- lettercnt[7];
                        -- lettercnt[19];
                        ++ digitcnt[digit];
                    }
                    break;
                case 9:
                    while(lettercnt[8] > 0){
                        -- lettercnt[8];
                        -- lettercnt[4];
                        lettercnt[13] -= 2;
                        ++ digitcnt[digit];
                    }
                    break;
            }
        }
    private:
        int digitorder[10]={0,2,6,8,4,1,5,7,9,3};
    };
    

Log in to reply
 

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