Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

92. Decode String

MediumDay 27 of 60Stack & Queue

You are given an encoded string where k[text] means text repeated exactly k times, and such patterns can be nested. Return the decoded string. The input is always well formed, k is a positive integer, and digits only appear as repeat counts (plain text never contains digits).

Example 1
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
Example 2
Input: s = "3[a2[c]]"
Output: "accaccacc"
The inner 2[c] becomes cc, so the outer part is 3[acc].
Constraints
  • 1 <= s.length <= 30
  • `s` has lowercase letters, digits and square brackets
  • 1 <= k <= 300
  • The output length is at most 10^5
Hints
Frequently asked at: Google, Amazon, Microsoft, Atlassian
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min