Delete the Fewest Characters to Avoid Triple Repeats

Quick Overview

Delete the fewest lowercase characters needed to eliminate every run of three identical consecutive letters while preserving the relative order of retained characters.

Delete the Fewest Characters to Avoid Triple Repeats

Company: DRW

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Online Assessment

## Problem Given a string of lowercase English letters, delete the minimum possible number of characters so that the result contains no run of three identical consecutive letters. Return the resulting string. The relative order of every retained character must remain unchanged. ### Function Contract Implement `removeTripleRepeats(text)` and return a string. ### Constraints & Assumptions - `1 <= len(text) <= 200,000`. - `text` contains only `a` through `z`. - Deletion is the only allowed edit. - The minimum-deletion result is unique: each original run of length `r` contributes its first `min(r, 2)` characters. ### Clarifying Questions to Ask - Can the same letter appear more than twice in the result if another letter separates occurrences? Yes. - Are replacements or reordering allowed? No. - Should two identical consecutive letters be retained? Yes. - Is a new invalid run ever created by deleting from a run? No, because deleted characters have the same value as their retained neighbors. ```hint Decide each character from the output suffix Append the current character unless the last two retained characters are both equal to it. ``` ### Examples - `"eedaaad"` returns `"eedaad"`. - `"xxxtxxx"` returns `"xxtxx"`. - `"uuuuxaaaaxuuu"` returns `"uuxaaxuu"`. - `"ab"` returns `"ab"`. ### Evaluation Focus - Removes exactly the excess characters from every maximal run. - Preserves order and does not over-delete a run of length one or two. - Handles very long single-character runs in one pass. - Runs in `O(n)` time with `O(n)` output storage. ### Extensions to Discuss 1. How would the implementation change if up to `m` identical consecutive letters were allowed? 2. Can the operation be performed on a character stream without storing the input? 3. How would you return the deleted source indices as well as the repaired string?

Quick Answer: Delete the fewest lowercase characters needed to eliminate every run of three identical consecutive letters while preserving the relative order of retained characters.

|Home/Coding & Algorithms/DRW
DRW logo
DRW
May 22, 2026, 12:00 AM
easyData EngineerOnline AssessmentCoding & Algorithms
0
0

Problem

Given a string of lowercase English letters, delete the minimum possible number of characters so that the result contains no run of three identical consecutive letters. Return the resulting string.

The relative order of every retained character must remain unchanged.

Function Contract

Implement removeTripleRepeats(text) and return a string.

Constraints & Assumptions

  • 1 <= len(text) <= 200,000 .
  • text contains only a through z .
  • Deletion is the only allowed edit.
  • The minimum-deletion result is unique: each original run of length r contributes its first min(r, 2) characters.

Clarifying Questions to Ask Guidance

  • Can the same letter appear more than twice in the result if another letter separates occurrences? Yes.
  • Are replacements or reordering allowed? No.
  • Should two identical consecutive letters be retained? Yes.
  • Is a new invalid run ever created by deleting from a run? No, because deleted characters have the same value as their retained neighbors.

Examples

  • "eedaaad" returns "eedaad" .
  • "xxxtxxx" returns "xxtxx" .
  • "uuuuxaaaaxuuu" returns "uuxaaxuu" .
  • "ab" returns "ab" .

Evaluation Focus

  • Removes exactly the excess characters from every maximal run.
  • Preserves order and does not over-delete a run of length one or two.
  • Handles very long single-character runs in one pass.
  • Runs in O(n) time with O(n) output storage.

Extensions to Discuss

  1. How would the implementation change if up to m identical consecutive letters were allowed?
  2. Can the operation be performed on a character stream without storing the input?
  3. How would you return the deleted source indices as well as the repaired string?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...