An inventory system compresses repeated label sequences with a compact code. The code consists of lowercase English letters, digits, [, and ], with no spaces. A run of one or more digits immediately followed by [ and a matching ] means: repeat everything between that [ and its matching ] that many times. The bracketed content may itself contain further such repeat-codes (nesting), and may also contain plain letters interspersed with them.
For example, 3[ab] decodes to ababab, and 2[a3[b]] decodes to abbbabbb (the inner 3[b] becomes bbb, giving a + bbb = abbb, repeated twice).
It is guaranteed that the code is well-formed (every [ has a matching ], every count is a positive integer with no leading zero, and there are no digits except immediately before a [), and that the fully decoded string never exceeds 1000 characters.
Line 1: the encoded string.
A single line: the fully decoded string.
Example 1
Input
3[ab]
Expected
ababab
Explanation
The group 'ab' is repeated 3 times: ababab.
Example 2
Input
2[a3[b]]
Expected
abbbabbb
Explanation
The inner group '3[b]' decodes to 'bbb', giving 'a'+'bbb'='abbb' inside, which is then repeated twice: abbbabbb.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →