Skip to content
RED SAGSkills
medium30 ptsstackstrings

Expand Repeat Blocks

A template language writes n[body] to mean body repeated n times, where n is a positive integer and body may contain further blocks. Letters outside blocks are copied as they are.

Given a valid template s, return the fully expanded string.

Example 1

Input: s = "3[ab]2[c]"
Output: "abababcc"

Example 2

Input: s = "2[a3[b]]"
Output: "abbbabbb"

Constraints

  • 0 <= s.length <= 100
  • The expanded string has at most 10^5 characters.
  • s contains lowercase letters, digits and brackets.
Hint 1

When you see [, save the text built so far and the repeat count on stacks.

Hint 2

On ], repeat the current text and append it to the saved text.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, DevOps Engineer

Run checks your code on 2 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.