Medium · Nested Stack Frames
Understand → Visualise → Practice.
Decode repetition expressions such as `3[a2[c]]`; inputs are valid and repetition counts are positive.
Nested brackets suggest a stack. At '[', push the repeat count and the text built so far; at ']', pop them and attach the repeated inner text.
Build the repeat count digit by digit (counts can have several digits).
At '[', push (count, current text) and start fresh.
At ']', pop and set current = previous text + current × count; letters are appended to current.
O(output length) time and O(nesting depth + output) space.
Reading only one digit breaks counts like 12[a].
Read the full guides first: Stacks, Queues and Deques.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.