Skip to interview prep
Apexed

88. Decode a Nested Repeat String

MediumStacks

A string is encoded with the rule k[text], meaning text repeated k times. Encodings may be nested. Return the decoded string.

The input is always valid: digits appear only as repeat counts, every count is followed by brackets, and letters are lowercase.

Example 1

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

Example 2

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

Constraints

  • 1 ≤ s.length ≤ 30
  • 1 ≤ k ≤ 300
  • The decoded length is at most 10⁵

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Decode a Nested Repeat String — coding interview practice — Apexed