#394

Decode String

specialist · 675 · lc medium +30 · verified · 62.3% accepted · 13,932 likes · top 63%

play →

Description

An encoded string uses the format k[t] to mean substring t is repeated exactly k times (k is a positive integer). These encodings can nest to any depth.

The input is always well-formed: brackets are balanced, there are no spaces, and digits only serve as repetition counts (no input like 3a or 2[4]). The fully decoded string never exceeds 105 characters.

Return the fully decoded string.

Example 1:

Input: s = "3[a]2[bc]"
Output: "aaabcbc"

Example 2:

Input: s = "3[a2[c]]"
Output: "accaccacc"

Example 3:

Input: s = "2[abc]3[cd]ef"
Output: "abcabccdcdcdef"

Code

1
2
3