Medium Text

String Decoder

Receives a string encoded in the run-length format n[substring], where a number followed by a bracketed group means that group repeats n times, and groups can be nested. It scans the text left to right with a stack: digits build the repeat count, an opening bracket pushes the text built so far together with its count, plain letters are appended to the current text, and a closing bracket pops the saved text and count to repeat the just-finished group — expanding the innermost groups first. Returns the fully expanded string; text without brackets is returned unchanged and an empty input yields an empty string.

Visualization

Input

Algorithm code

Custom input

Saved inputs

References