Refactor a legacy decoder for strings of the form k[encoded_string]. Implement decode_string(s) so it returns the fully expanded string, handles nested brackets, and avoids inefficient repeated rescanning or string concatenation.
s = "3[a]2[bc]"Output"aaabcbc"Why`a` repeats 3 times and `bc` repeats 2 times.s = "3[a2[c]]"Output"accaccacc"WhyDecode the inner block first, then apply the outer repetition.`1 <= len(s) <= 10^5`Input contains lowercase letters, digits, `[` and `]`Repeat counts may be multi-digitInput is always valid