← Back to challenges

Casual String Builder

JavaScriptHardconditionsformattingregexstrings

Instructions

The function is given a string with some square brackets in it. You need to build the outcome string using the rule: k[substring] is replaced by the substring inside the square brackets being repeated exactly k times.

Examples

stringBuilder("3[a]2[bc]") ➞ "aaabcbc"

stringBuilder("3[a2[c]]") ➞ "accaccacc"

stringBuilder("2[abc]3[cd]ef") ➞ "abcabccdcdcdef"

Notes

k is a positive integer.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.