5 Best Ways to Find String After Deleting K Consecutive Duplicate Characters in Python

Rate this post
Delete Consecutive Duplicate Characters in Python

πŸ’‘ Problem Formulation: We are presented with the task of processing a string to eliminate any group of consecutive duplicate characters when the group’s size is exactly k. For instance, given the input string "aabbcccb" and k=3, the desired output would be "aabb" after removing the three consecutive 'c' characters. This article explores different methods to achieve this in Python.

Method 1: Using Stack

One effective approach to solving this problem is to use a stack data structure. We iterate over the string and push characters onto the stack while keeping track of the count of consecutive duplicates. When we encounter k duplicates, we remove them from the stack. This algorithm is both intuitive and efficient for strings of considerable length.

Here’s an example:

def remove_duplicates(input_string, k):
    stack = []
    for char in input_string:
        if stack and stack[-1][0] == char:
            stack[-1][1] += 1
            if stack[-1][1] == k:
            stack.append([char, 1])
    return ''.join(char * count for char, count in stack)

print(remove_duplicates("aabbcccb", 3))

Output: aabb

This code defines a function remove_duplicates which takes an input string and an integer k. It iterates through each character in the string, using a stack to track characters and their counts. When a count reaches k, the group is removed. The result is the input string minus any sequences of k consecutive identical characters.

Method 2: Recursive Approach

The recursive method hinges on the idea that if we find k consecutive duplicate characters, we can perform a recursive call to process the string after removing this group. Although elegant, this method may not be optimal for very long strings due to recursion depth limits and potential performance issues.

Here’s an example:

def remove_duplicates_recursive(input_string, k):
    for i in range(len(input_string) - k + 1):
        if input_string[i] == input_string[i + 1] and all(input_string[i] == input_string[j] for j in range(i, i + k)):
            return remove_duplicates_recursive(input_string[:i] + input_string[i+k:], k)
    return input_string

print(remove_duplicates_recursive("aabbcccb", 3))

Output: aabb

The function remove_duplicates_recursive processes the string by searching for k consecutive duplicate characters and calling itself without those characters if found. It continues this process until no such group exists in the string.

Method 3: Iterative Approach

An iterative method involves sequentially checking for k consecutive characters that are the same and removing them, then checking the string again from the beginning until no more removals occur. This is more memory-efficient but could potentially lead to higher time complexity with large strings and frequent duplications.

Here’s an example:

def remove_duplicates_iterative(input_string, k):
    i = 0
    while i < len(input_string) - k + 1:
        if all(input_string[i] == input_string[i+j] for j in range(k)):
            input_string = input_string[:i] + input_string[i+k:]
            i = 0
            i += 1
    return input_string

print(remove_duplicates_iterative("aabbcccb", 3))

Output: aabb

This function defines an iterative procedure to check for k consecutive duplicates and removes them, resetting the search to the beginning of the string each time a removal is made.

Method 4: Regular Expressions

Regular expressions can be utilized to match k consecutive duplicate characters easily, which can then be removed using the sub() function from the re module. This method is concise and clear but may not be as efficient for large strings or complex patterns.

Here’s an example:

import re

def remove_duplicates_regex(input_string, k):
    pattern = r"(.)\1{" + str(k-1) + "}"
    return re.sub(pattern, '', input_string)

print(remove_duplicates_regex("aabbcccb", 3))

Output: aabb

Here, remove_duplicates_regex utilizes Python’s re module to find and substitute out k consecutive duplicate characters. The pattern is dynamically built to match exactly k duplications of any character, which is then replaced with an empty string.

Bonus One-Liner Method 5: Functional Approach

This minimalistic approach relies on the power of Python’s functional programming tools like the groupby function from itertools. It’s a succinct and ‘Pythonic’ way to solve the problem in a single line of code using a generator expression. However, it may not be immediately clear to those unfamiliar with functional programming paradigms.

Here’s an example:

from itertools import groupby

print(''.join(''.join(group) for key, group in groupby("aabbcccb") if len(list(group)) != 3))

Output: aabb

Using itertools.groupby, this one-liner creates groups of consecutive duplicates and only rejoins those groups to the result string if their length is not k. It’s an elegant solution that leverages Python’s standard library.


  • Method 1: Stack. Efficient with space and time complexity. Works well with long strings. Does not involve recursion, hence no stack overflow risks.
  • Method 2: Recursive Approach. Elegant and simple code. However, it can hit recursion depth limits and may be inefficient for strings with a large number of duplicate sequences.
  • Method 3: Iterative Approach. Memory-efficient and clear logic. But may run into higher time complexity with longer strings or strings with frequent duplicates.
  • Method 4: Regular Expressions. Clean and concise. Excellent for simple patterns but potentially less efficient with large data sets or complex regular expressions.
  • Method 5: Functional Approach. Highly concise and Pythonic. However, may not be intuitive for those unfamiliar with functional programming in Python.