Skip to content
Onsite

Decode String

MediumP2Coding

An encoded string uses the rule k[encoded], meaning encoded is repeated exactly k times. Rules can be nested. Return the decoded string.

The input is always valid: brackets match, k is a positive integer written in digits, and digits appear only as repeat counts (never as plain text).

Example 1

Input
s = "3[a]2[bc]"
Output
"aaabcbc"

Example 2

Input
s = "3[a2[c]]"
Output
"accaccacc"

Example 3

Input
s = "2[abc]3[cd]ef"
Output
"abcabccdcdcdef"

Constraints

  • 1 <= s.length <= 30
  • s has lowercase letters, digits and square brackets
  • Every repeat count is in [1, 300]
  • The decoded length is at most 10^5

Hints

Run checks the 3 visible tests, Submit all 10.