Erase Equal Adjacent Runs of Size k

Quick Overview

Erase Equal Adjacent Runs of Size k. Repeatedly remove k adjacent equal letters from s, joining the two surrounding pieces after every removal.

Erase Equal Adjacent Runs of Size k

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Repeatedly remove k adjacent equal letters from s, joining the two surrounding pieces after every removal. Return the final string when no removal remains. The source guarantees the final string is unique. ```cpp class Solution { public: string removeDuplicates(string s, int k); }; ``` ### Examples **Example 1:** ```text Input: s = "aabbbac", k = 3 Output: "c" Explanation: Removing bbb joins the a letters into aaa, which then disappears. ``` **Example 2:** ```text Input: s = "aaaa", k = 3 Output: "a" Explanation: One group of three disappears and one a remains. ``` ### Constraints - The input has 1 to 100000 lowercase English letters. - k is an integer from 2 through 10000. ### Contract considerations No choice of removal order needs to be returned; the final string is deterministic. ```hint Read the boundary condition Can removing a group create a new eligible group across the gap, and may the final result be empty? ``` ### Boundary reminders No choice of removal order needs to be returned; the final string is deterministic.

Overview: Erase Equal Adjacent Runs of Size k. Repeatedly remove k adjacent equal letters from s, joining the two surrounding pieces after every removal.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Repeatedly remove k adjacent equal letters from s, joining the two surrounding pieces after every removal. Return the final string when no removal remains. The source guarantees the final string is unique.

class Solution {
public:
    string removeDuplicates(string s, int k);
};

Examples

Example 1:

Input: s = "aabbbac", k = 3
Output: "c"
Explanation: Removing bbb joins the a letters into aaa, which then disappears.

Example 2:

Input: s = "aaaa", k = 3
Output: "a"
Explanation: One group of three disappears and one a remains.

Constraints

  • The input has 1 to 100000 lowercase English letters.
  • k is an integer from 2 through 10000.

Contract considerations

No choice of removal order needs to be returned; the final string is deterministic.

Boundary reminders

No choice of removal order needs to be returned; the final string is deterministic.