dsa · medium

Decode String

An encoded string is written as k[encoded_string], where the part inside the brackets is repeated exactly k times. Brackets nest, and a repetition count is always followed by an opening bracket. Plain letters outside any bracket appear as themselves. Expand the encoding and return the decoded string.

Arguments

Example

s = "3[a2[c]]"

Inner first: a2[c] expands to acc. Then repeat that 3 times → accaccacc.

s = "2[abc]3[cd]ef" expands to abcabccdcdcdef.

Constraints

1 <= s.length <= 30 s consists of digits, letters, and brackets the decoded string is at most 10^4 characters Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
"3[a]2[bc]"

Expected:
aaabcbc

Example 2

Input:
"3[a2[c]]"

Expected:
accaccacc

Example 3

Input:
"2[abc]3[cd]ef"

Expected:
abcabccdcdcdef

Open in the Dojo editor