Accepted C++ solution


  • 0
    I
    class Solution {
    public:
        bool findOccurences(vector<int> &v, string s)
        {
            for(int i = 0; i < s.size(); ++i)
            {
                --v[s[i]];
                if(v[s[i]] < 0)
                {
                    for(int j = 0; j <= i; ++j)
                       ++v[s[j]];
                    return false;
                }
            }
            return true;
        }
        string originalDigits(string s) {
            vector<int> v(256,0);
            for(int i = 0; i < s.size(); ++i)
              ++v[s[i]];
            string res0;
            string res1;
            string res2;
            string res3;
            string res4;
            string res5;
            string res6;
            string res7;
            string res8;
            string res9;
            while(findOccurences(v,"eight"))
               res8.push_back('8');
            while(findOccurences(v,"six"))
               res6.push_back('6');
            while(findOccurences(v,"seven"))
               res7.push_back('7');
            while(findOccurences(v,"zero"))
               res0.push_back('0');
            while(findOccurences(v,"two"))
               res2.push_back('2');
            while(findOccurences(v,"three"))
               res3.push_back('3');
            while(findOccurences(v,"four"))
               res4.push_back('4');
            while(findOccurences(v,"five"))
               res5.push_back('5');
            while(findOccurences(v,"nine"))
               res9.push_back('9');
            while(findOccurences(v,"one"))
               res1.push_back('1');
            return res0 + res1 + res2 + res3 + res4 + res5 + res6 + res7 + res8 + res9;
    
        }
    };
    

    Please keep in mind that the order of findOccurences calls matters.
    https://discuss.leetcode.com/topic/63376/what-is-wrong-with-this-code


Log in to reply
 

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