class Solution { public: string decodeString(string s) { stack<pair<int, string>> stk; int multi = 0; string pre = ""; for(auto c : s) { if(c >= '0' && c <= '9') { if(!multi) multi = c - 48; else { multi *= 10; multi += c - 48; } } else if (c == '[') { stk.push({multi, pre}); multi = 0; pre = ""; } else if (c == ']') { auto p = stk.top(); stk.pop(); string tm = pre; pre = p.second; for(int i = 0; i < p.first; ++i) pre += tm; } else { pre += c; } } return pre; } };