Decode an encoded string where counts and brackets describe repeated substrings.
You are given an encoded string containing lowercase letters, digits, and square brackets. The string uses the format k[encoded_string], where the substring inside the brackets should be repeated k times.
The encoding can be nested, meaning an expanded substring may itself contain more encoded parts.
Return the fully decoded string.
k is always a positive integer.s.s contains lowercase English letters, digits, and the characters [ and ].Example 1
Input
s = "3[a]2[bc]"
Output
"aaabcbc"
Explanation
a is repeated 3 times, and bc is repeated 2 times. Concatenate the results: aaa + bcbc = aaabcbc.
Example 2
Input
s = "3[a2[c]]"
Output
"accaccacc"
Explanation
First decode the inner part 2[c] as cc, then expand a2[c] to acc, and finally repeat it 3 times.
Example 3
Input
s = "2[abc]3[cd]ef"
Output
"abcabccdcdcdef"
Explanation
Expand each bracketed segment and keep the trailing characters ef unchanged.
Premium problem context
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.