Repeatedly Delete Adjacent Duplicate Pairs or Whole Runs of Equal Characters
Company: Whatnot
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Overview: A string coding problem with two modes: repeatedly delete pairs of equal adjacent characters, or, as the follow-up, delete whole runs of equal adjacent characters, always the leftmost run first, and return what remains. It tests simulating cascading deletions efficiently and handling runs that merge after a deletion.
Constraints
- 1 <= len(s) <= 10^5
- s consists only of lowercase English letters a to z.
- remove_whole_runs is True or False.
Examples
Input: ('abbaca', False)
Expected Output: 'ca'
Explanation: Source example 1: deleting 'bb' makes the two a's adjacent, that pair is deleted too, leaving 'ca'.
Input: ('abbaa', True)
Expected Output: ''
Explanation: Source example 3, leftmost rule: 'bb' goes first, then the merged run 'aaa'; deleting both runs at once or the right run first would leave 'a'.
Hints
- After any deletion, only the two characters that border the removed block become newly adjacent. Which part of the string can still change, and which part is already final?
- In whole-run mode, everything to the left of the leftmost run of length two or more has no two equal neighbours. Where, relative to what you have already read, can the next deletion happen?
- Work through "abbaa" in both modes by hand and compare with the examples; the whole-run result depends on deleting the leftmost run first.