Repeatedly Delete Runs of k Adjacent Equal Characters from a String
Company: Oracle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Given a lowercase string and an integer k, repeatedly delete k adjacent equal characters, letting the remaining parts join and possibly form new runs, until no deletion is possible, then return what remains. It tests efficient simulation of cascading removals on strings of up to 100,000 characters.
Constraints
- 1 <= len(s) <= 10^5
- 2 <= k <= 10^4
- s contains only lowercase English letters 'a' to 'z'.
Examples
Input: ('abbbaac', 3)
Expected Output: 'c'
Explanation: Source example 1: removing 'bbb' joins the a's into 'aaa', which is removed, leaving 'c'.
Input: ('xyyzzzyx', 2)
Expected Output: 'xzyx'
Explanation: Source example 2: 'yy' is removed and the run 'zzz' loses two characters, leaving one z.
Hints
- Because the final string does not depend on the order of removals, you are free to perform them in whatever order is most convenient.
- A run of L equal letters that never joins anything else leaves L mod k of them behind.
- After a removal, only the characters immediately on either side of the gap can form a new run, so the next removal can only happen where the previous one was.