Decode Nested Repetition Strings with Stacks
Company: Compass
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Decode nested repetition syntax with saved prefix and count frames. Handle multi-digit counts, prove the stack invariant, and account honestly for expanded output and repeated string copying.
Constraints
- Valid bracket syntax with lowercase literal letters.
- Repeat counts are positive integers from 1 to 300; digits only occur in repeat counts.
- Encoded length is 1 through 30, and decoded length is at most 100000.
Examples
Input: ('2[a3[b]]c',)
Expected Output: 'abbbabbbc'
Explanation: The inner three b characters are duplicated with each outer a.
Input: ('3[a]',)
Expected Output: 'aaa'
Explanation: One simple repetition.
Hints
- Save the current context when a new bracket opens.
- A count may contain more than one digit.