A shipping system compresses repetitive labels with a bracket encoding. The encoding rule is k[encoded], meaning the decoded encoded string is repeated exactly k times, where k is a positive integer. Encodings may be nested, and plain lowercase letters outside any brackets are copied through unchanged. For example 3[a]2[bc] decodes to aaabcbc, and 2[a2[b]] decodes to abbabb.
The input is guaranteed to be a well-formed encoding: brackets are balanced, every [ is immediately preceded by a positive integer, and only lowercase letters appear inside as literal characters.
A single line: the encoded string (it may be empty).
A single line: the fully decoded string (an empty line if the decoding is empty).
k satisfies 1 <= k <= 100.Example 1
Input
3[ab]
Expected
ababab
Explanation
The substring ab is repeated 3 times, giving ababab.
Example 2
Input
2[a2[bc]]
Expected
abcbcabcbc
Explanation
Innermost 2[bc] is bcbc, so a2[bc] decodes to abcbc, and repeating that twice gives abcbcabcbc.
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 →