Another C++ Recursive Solution


  • 1
    N

    '''
    class Solution {
    public:

    string pushPopDecodeString(string& s, size_t& idx)
    {
        int num;
        string out, strNum;
        while(idx < s.size())
        {
            if(s[idx] == '[')
            {
                string rtStr = pushPopDecodeString(s, ++idx);
                num = stoi(strNum);
                strNum.clear();
                for(int i = 0; i < num; i++)
                    out +=  rtStr;
            }
            else if(s[idx] == ']')
            {
                idx++;
                return out;
            }
            else if (isdigit(s[idx]))
                strNum += s[idx++];
            else
                out += s[idx++];
                
        }
        
        return out;
    }
    string decodeString(string s) 
    {
        size_t idx = 0;
        return pushPopDecodeString(s, idx);
        
    }
    

    };
    '''


Log in to reply
 

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