Open In App

Python – Remove K length Duplicates from String

Last Updated : 02 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given a String remove all K length duplicates.

Input : test_str = ‘geeksforfreeksfo’, K = 3
Output : geeksforfree
Explanation : eek, eks, ksf, sfo already in string, hence removed.

Input : test_str = ‘geeksforg’, K = 3
Output : geeksforg
Explanation : No repeated string, nothing removed.

Method : Using loop + slicing

In this, we keep track of all the K length substrs encountered, extracted using slicing, and check each time for recurrence, if occurred they are removed.

Python3




# Python3 code to demonstrate working of 
# Remove K length Duplicates from String
# Using loop + slicing 
  
# initializing strings
test_str = 'geeksforfreeksfo'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing K 
K = 3
  
memo = set()
res = []
for idx in range(0, len(test_str) - K):
      
    # slicing K length substrings
    sub = test_str[idx : idx + K]
      
    # checking for presence
    if sub not in memo:
        memo.add(sub)
        res.append(sub)
          
res = ''.join(res[ele] for ele in range(0, len(res), K))
  
# printing result 
print("The modified string : " + str(res)) 


Output

The original string is : geeksforfreeksfo
The modified string : geeksforfree

Similar Reads

Python Program To Remove Duplicates From A Given String
Write a Python program for a given string S which may contain lowercase and uppercase characters. The task is to remove all duplicate characters from the string and find the resultant string. Note: The order of remaining characters in the output should be the same as in the original string.Example: Input: Str = geeksforgeeksOutput: geksforExplanati
2 min read
Remove All Duplicates from a Given String in Python
We are given a string and we need to remove all duplicates from it. What will be the output if the order of character matters? Examples: Input : geeksforgeeks Output : geksfor This problem has an existing solution please refer to Remove all duplicates from a given string. Method 1: [GFGTABS] Python from collections import OrderedDict # Function to
3 min read
Python Remove Duplicates from a List
The job is simple. We need to take a list, with duplicate elements in it and generate another list that only contains the element without the duplicates in them. Examples: Input : [2, 4, 10, 20, 5, 2, 20, 4] Output : [2, 4, 10, 20, 5] Input : [28, 42, 28, 16, 90, 42, 42, 28] Output : [28, 42, 16, 90] We can use not in on list to find out the duplic
3 min read
Python groupby method to remove all consecutive duplicates
Given a string S, remove all the consecutive duplicates. Examples: Input : aaaaabbbbbb Output : ab Input : geeksforgeeks Output : geksforgeks Input : aabccba Output : abcba We have existing solution for this problem please refer Remove all consecutive duplicates from the string link. We can solve this problem in python quickly using itertools.group
2 min read
Python | Remove all duplicates words from a given sentence
Given a sentence containing n words/strings. Remove all duplicates words/strings which are similar to each others. Examples: Input : Geeks for Geeks Output : Geeks for Input : Python is great and Java is also great Output : is also Java Python and great We can solve this problem quickly using python Counter() method. Approach is very simple. 1) Spl
7 min read
Python | Remove consecutive duplicates from list
In Python, we generally wish to remove the duplicate elements, but sometimes for several specific usecases, we require to have remove just the elements repeated in succession. This is a quite easy task and having a shorthand for it can be useful. Let's discuss certain ways in which this task can be performed. Method #1 : Using groupby() + list comp
4 min read
Python | Remove all duplicates and permutations in nested list
Given a nested list, the task is to remove all duplicates and permutations in that nested list. Input: [[-11, 0, 11], [-11, 11, 0], [-11, 0, 11], [-11, 2, -11], [-11, 2, -11], [-11, -11, 2]] Output: {(-11, 0, 11), (-11, -11, 2)} Input: [[-1, 5, 3], [3, 5, 0], [-1, 5, 3], [1, 3, 5], [-1, 3, 5], [5, -1, 3]] Output: {(1, 3, 5), (0, 3, 5), (-1, 3, 5)}
4 min read
Python | Remove duplicates from nested list
The task of removing duplicates many times in the recent past, but sometimes when we deal with the complex data structure, in those cases we need different techniques to handle this type of problem. Let's discuss certain ways in which this task can be achieved. Method #1 : Using sorted() + set() This particular problem can be solved using the above
5 min read
Python | Sort given list by frequency and remove duplicates
Problems associated with sorting and removal of duplicates is quite common in development domain and general coding as well. The sorting by frequency has been discussed, but sometimes, we even wish to remove the duplicates without using more LOC's and in a shorter way. Let's discuss certain ways in which this can be done. Method #1 : Using count()
5 min read
Python | Remove duplicates in Matrix
While working with Python Matrix, we can face a problem in which we need to perform the removal of duplicates from Matrix. This problem can occur in Machine Learning domain because of extensive usage of matrices. Let's discuss certain way in which this task can be performed. Method : Using loop This task can be performed in brute force manner using
2 min read
Practice Tags :
three90RightbarBannerImg