c++ recursive solution


  • 0
    B
        string decodeString(string s) {
            auto found = s.find_last_of('[');
            if (found == string::npos) {
                return s;
            } else {
                auto iNum = found;
                while (isdigit(s[iNum - 1])) iNum--;
                int num = stoi(s.substr(iNum, found - iNum));
                auto foundEnd = s.find_first_of(']', found);
                string mid = s.substr(found + 1, foundEnd - found - 1);
                string midsum;
                while (num-- > 0) midsum += mid;
                return decodeString(s.substr(0, iNum) + midsum + s.substr(foundEnd + 1));
            }
        }
    

Log in to reply
 

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