Loading...
You are given an encoded string s. The encoding rule is k[inner], meaning the string inner repeated exactly k times. Repeat blocks may be nested, as in 3[a2[c]], and may sit alongside ordinary letters.
Return the expanded string.
You can assume the input is always valid: brackets are well formed, there are no spaces, and digits only ever appear as repeat counts, so inputs like 3a or 2[4] never occur. The expanded result is guaranteed to be at most 105 characters long.
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
Explanation: "a" repeats three times and "bc" repeats twice.
Input: s = "3[a2[c]]"
Output: "accaccacc"
Explanation: The inner block expands to "cc" first, making "acc", which then repeats three times.
Input: s = "2[abc]3[cd]ef"
Output: "abcabccdcdcdef"
Explanation: Two repeat blocks followed by the literal "ef".
s.length ≤30s consists of lowercase English letters, digits, and the brackets [ and ]s is guaranteed to be a valid encoding.s is in the range [1,300].Click "Run" to test with sample cases or "Submit" to run all tests.