Decode an encoded string
InstacartSoftware Engineer · Technical ScreenLast updated
This question evaluates string parsing and manipulation skills, particularly handling nested repetition encodings and managing state when expanding bracketed substrings.
Implement a decoder for an encoded string where repetition is expressed with a number followed by a bracketed substring.
Rules:
- A pattern
k[substring]meanssubstringrepeatedktimes. - Patterns can be nested.
- Characters outside brackets are literal.
kis a positive integer.
Examples:
- Input:
"3[a]2[bc]"→ Output:"aaabcbc" - Input:
"3[a2[c]]"→ Output:"accaccacc" - Input:
"2[ab3[c]]x"→ Output:"abcccabcccx"
Return the decoded string.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.