Quick Overview

Repeatedly remove adjacent runs of k equal characters until no further cascade is possible. Use a stack of character counts for linear time, handling long runs, complete removal, empty input, and the k = 2 special case.

Remove Adjacent Duplicate Runs of Length K

Company: Attentive

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Remove Adjacent Duplicate Runs of Length K Given a string `s` and an integer `k`, repeatedly remove any group of `k` equal adjacent characters. A removal can make characters on its two sides adjacent and trigger another removal. Return the final string after no more groups can be removed. For example: ```text s = "deeedbbcccbdaa", k = 3 output = "aa" ``` The original duplicate-removal case is `k = 2`; the implementation should handle the general `k` case directly. ### Constraints & Assumptions - `k` is at least `2`. - A run longer than `k` is processed according to the same repeated-removal rule. - The result must reflect all cascading removals, not just runs present in the original string. ### Clarifying Questions to Ask - Is `k` fixed for the lifetime of the program or supplied for each call? - What should be returned for an empty input string? - Are characters compared by exact code point and case? ```hint Store the current run length Keep each surviving character group together with its count so a new character updates only the top group. ``` ```hint Let removals expose prior state When a count reaches `k`, removing that group should reveal the previous group without rescanning the whole output. ``` ### Evaluation Criteria - Correct handling of cascading removals and runs longer than `k`. - A stack of character/count pairs or an equivalent one-pass representation. - Linear time in the input length and linear worst-case auxiliary space. - Edge cases including an empty string, no removable run, and complete removal. ### Extensions to Discuss - How does the implementation simplify when `k = 2`? - Can the result be produced online if the input arrives as a stream? - What changes if each character has its own removal threshold?

Quick Answer: Repeatedly remove adjacent runs of k equal characters until no further cascade is possible. Use a stack of character counts for linear time, handling long runs, complete removal, empty input, and the k = 2 special case.

Repeatedly remove any group of k equal adjacent characters. Each removal may join two surviving groups and trigger another removal; return the string after no more groups of k remain.

Constraints

  • 0 <= text.length <= 10,000.
  • 2 <= k <= 10,000.
  • The console input uses printable ASCII characters compared case-sensitively.
  • Runs longer than k follow the same repeated-removal rule, including all cascades.

Examples

Input: ('', 2)

Expected Output: ''

Explanation: Empty input remains empty.

Input: ('abcd', 2)

Expected Output: 'abcd'

Explanation: No adjacent duplicate run is removed.

Hints

  1. Keep the count of the current surviving character group with that group.
  2. When a group reaches k, removing it should immediately expose the prior surviving state without a rescan.

Loading coding console...