visual walkthrough
Decode String
Nested brackets handled with a stack of (string, count) frames.
The idea
Brackets can nest, so when a group opens you must remember what you were building and how many times the new group will repeat. A stack of (text so far, repeat count) does that.
On the closing bracket, pop the saved pair, repeat what was just built, and attach it to the saved text.
Complexity
| approach | time | space |
|---|---|---|
| Stack of (text, count) | O(output length) | O(depth) |