This is one of those problems where the clean idea matters more than the amount of code. For 394. Decode String, the solution in this repository is mainly a stack solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack.
The notes already sitting in the source point us in the right direction:
- https://leetcode.com/problems/decode-string/discuss/87662/Python-solution-using-stack
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are decodeString.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//https://leetcode.com/problems/decode-string/discuss/87662/Python-solution-using-stack
02//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Decode String.
03//Memory Usage: 6.8 MB, less than 9.68% of C++ online submissions for Decode String.
04class Solution {
05public:
06 string decodeString(string s) {
07 stack<string> stks;
08 stack<int> stki;
09
10 string curs = "";
11 int curi = 0;
12
13 for(char& c : s){
14 if(c == '['){
15 stks.push(curs);
16 stki.push(curi);
17 curs = "";
18 curi = 0;
19 }else if(c == ']'){
20 /*
21 between [ and ], there is only chars,
22 so here we only use curs, not curi
23
24 we can see the string until ] as:
25 pres + prei[curs],
26 and we need to set curs as the parsed string
27 */
28 string pres = stks.top(); stks.pop();
29 int prei = stki.top(); stki.pop();
30
31 string tmp;
32 for(int i = 0; i < prei; ++i){
33 tmp += curs;
34 }
35 curs = pres + tmp;
36 }else if(isdigit(c)){
37 curi = curi*10 + (c-'0');
38 }else{
39 curs += c;
40 }
41 }
42
43 return curs;
44 }
45};
Cost