Python - Remove K length Duplicates from String Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report To remove consecutive K-length duplicates from a string iterate through the string comparing each substring with the next and excluding duplicates. For example we are given a string s = "abcabcabcabc" we need to remove k length duplicate from the string so that the output should become "aaaabc" . We can use methods like Counter from collection , string replacement, list comprehension .Using collections.CounterMethod counts substrings of length K and removes those that appear exactly K times. Python from collections import Counter s = "abcabcabcabc" k = 3 # Count the occurrences of each substring of length K sub_c = Counter(s[i:i+k] for i in range(len(s) - k + 1)) # Remove substrings that appear exactly K times res = ''.join([s[i] for i in range(len(s) - k + 1) if sub_c[s[i:i+k]] != k] + [s[i] for i in range(len(s)-k+1, len(s))]) print(res) Outputaaaabc Explanation:Counter() counts occurrences of all substrings of length K in the string s.New string is built by excluding substrings that appear exactly K times using a condition.Using String ReplacementMethod uses the replace() function to remove K-length duplicates explicitly. Python s = "abcabcabcabc" k = 3 # Loop to find and remove duplicates of length K for i in range(len(s) - k + 1): if s[i:i+k] == s[i+k:i+2*k]: # Check for consecutive K-length duplicates s = s[:i+k] + s[i+2*k:] print(s) Outputabcabc Explanation:Loop iterates through the string, checking if two consecutive substrings of length K are identical using s[i:i+K] == s[i+K:i+2*K].Consecutive duplicates are found string is updated by removing the second duplicate substring using slicing (s[:i+K] + s[i+2*K:])Using Set and Sliding WindowThis method uses a sliding window to keep track of substrings and removes duplicates by checking if the substring appears more than once. Python s = "abcabcabcabc" k = 3 res = [] seen = set() # Traverse the string with a sliding window of size K for i in range(len(s) - k + 1): sub = s[i:i+k] if sub not in seen: res.append(sub) seen.add(sub) print(''.join(res)) Outputabcbcacab Explanation:Loop iterates through the string, checking if two consecutive substrings of length K are identical using s[i:i+K] == s[i+K:i+2*K].Consecutive duplicates are found string is updated by removing the second duplicate substring using slicing (s[:i+K] + s[i+2*K:])Using List ComprehensionThis method checks for duplicate substrings of length K and removes them by comparing substrings in a list comprehension. Python s = "abcabcabcabc" k = 3 # Using list comprehension to remove K-length duplicates res = ''.join([s[i:i+k] for i in range(len(s) - k + 1) if s[i:i+k] not in s[i+k:i+2*k]]) print(res) Outputbcacababc Explanation:List comprehension iterates over the string, extracting substrings of length K and checks if each substring is not repeated in the next consecutive substring of the same length.Substrings that are not duplicates are joined together using ''.join(), forming the final string without consecutive duplicates of length K. Create Quiz Comment M manjeet_04 Follow 1 Improve M manjeet_04 Follow 1 Improve Article Tags : Python Python Programs Python string-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like