Expand a Nested Repeat-Count Encoded String
Company: Abridge
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Online Assessment
Overview: From an Abridge online assessment: decode a string in which k[text] means the bracketed text repeated k times, with multi-digit counts, nested brackets and plain letters between encoded parts. It tests parsing nested structure correctly and building the expanded output efficiently.
Constraints
- 1 <= len(s) <= 30
- s contains only lowercase English letters, digits, '[' and ']'.
- Every repeat count k satisfies 1 <= k <= 300.
- The decoded string has length at most 100,000.
- s is always valid: brackets are balanced, every '[' is immediately preceded by its repeat count, every repeat count is immediately followed by '[', and no pair of brackets is empty.
- Digits appear only as repeat counts, and a repeat count has no leading zeros.
Examples
Input: ('a',)
Expected Output: 'a'
Explanation: Minimum length: a single plain letter is copied unchanged.
Input: ('1[a]',)
Expected Output: 'a'
Explanation: Smallest encoded form; k = 1 repeats the text exactly once.
Hints
- A repeat count can have more than one digit, so read the whole number before acting on the '[' that follows it.
- When a ']' closes a group, only the text decoded since its matching '[' is repeated; the text decoded before that '[' has to be kept so the repeated part can be attached to it.
- A letter that comes right after a ']' belongs to whatever encloses the group that just closed, either an outer group or the top-level string.