Decode an encoded string where patterns of the form repeat the enclosed substring times.
Description
Given an encoded string, return its decoded string.
The encoding rule is: , where the
clike
encoded_stringclike
$k$You may assume that the input string is always valid; no extra white spaces, square brackets are well-formed, and no invalid characters are present. The original data does not contain any digits and digits are only for those repeat numbers, . For example, there will not be an input like
clike
3aclike
2[4]The decoded string may be long, but it is guaranteed to fit in a 32-bit signed integer length.
Input Format
A single encoded string .
Output Format
Return the decoded string.
Constraints
- The input string is valid.
- Digits appear only as repeat counts .
- The decoded result fits in a 32-bit signed integer length.
Example 1
Input
s = "3[a]2[bc]"
Output
"aaabcbc"
Explanation
clike
3[a]clike
aaaclike
2[bc]clike
bcbcclike
aaabcbcExample 2
Input
s = "3[a2[c]]"
Output
"accaccacc"
Explanation
Inner
clike
2[c]clike
ccclike
3[a2[c]]clike
3[acc]clike
accaccaccShow 1 more example
Example 3
Input
s = "2[abc]3[cd]ef"
Output
"abcabccdcdcdef"
Explanation
clike
2[abc]clike
abcabcclike
3[cd]clike
cdcdcdclike
efPremium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.