Expand Nested Repetition Expressions
Company: Waymo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of string parsing and nested structure handling, including skills in recursion, stack-based parsing, tokenization, and managing repetition semantics.
Constraints
- 0 <= len(s) <= 10^5
- The input string is always valid according to the given grammar
- Each repetition count `k` is a positive integer
- The total length of the expanded output will not exceed 10^6 characters
Examples
Input: "abs(cs){3}g"
Expected Output: "abscscscsg"
Explanation: The group `cs` is repeated 3 times, giving `abs` + `cscscs` + `g`.
Input: "a(b(c){2}){2}"
Expected Output: "abccbcc"
Explanation: Inner group `(c){2}` becomes `cc`, so outer group becomes `bcc`, repeated twice after the initial `a`.
Input: ""
Expected Output: ""
Explanation: An empty string expands to an empty string.
Input: "plain"
Expected Output: "plain"
Explanation: There are no repetition groups, so the string stays unchanged.
Input: "(ab){10}"
Expected Output: "abababababababababab"
Explanation: The group `ab` is repeated 10 times. This checks correct handling of multi-digit counts.
Input: "xy((zz){1}yw){3}q"
Expected Output: "xyzzywzzywzzywq"
Explanation: Inner `(zz){1}` becomes `zz`, so the outer group becomes `zzyw`, repeated 3 times, with `xy` before it and `q` after it.
Input: "(a(b){2}c){2}d"
Expected Output: "abbcabbcd"
Explanation: Inside the outer group, `(b){2}` becomes `bb`, so the group is `abbc`, repeated twice, then followed by `d`.
Hints
- When you see `(`, solve the substring inside it first before applying the repetition count that follows `)`.
- Track your current position in the string so nested groups can be parsed without rescanning characters.