Decode String

Medium · Stack & Monotonic Stack · about 20 minutes

Asked at Bloomberg, Google, Amazon, Meta, ByteDance, eBay, Walmart Global Tech, Wayfair, Etsy, Shopee, Flipkart, Zalando.

Given an encoded string, return its decoded form. The rule is k[encoded]: the text inside the brackets is repeated exactly k times (k ≥ 1). Encodings can be nested, digits only appear as repeat counts, and the input is always well formed. For example "3[a2[c]]" decodes to "accaccacc".