
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find string after deleting k consecutive duplicate characters in python
Suppose we have a string s and another value k, we repeatedly delete the earliest k consecutive duplicate characters, and return the final string.
So, if the input is like s = "paaappmmmma" k = 3, then the output will be "ma", as when we delete three "a"s to get "pppmmmma". Then we delete three "p"s to get "mmmma". Then delete three of the four "m"s to get "ma".
To solve this, we will follow these steps:
- do the following steps infinitely, do
- count := 0
- chars := get the unique characters from s
- for each character c in chars, do
- if k consecutive c is in s, then
- delete k consecutive c from s
- count := count + 1
- if k consecutive c is in s, then
- if count is same as 0, then
- come out from the loop
- returns
Let us see the following implementation to get better understanding:
Example
class Solution: def solve(self, s, k): while True: count = 0 chars = set(s) for c in chars: if c * k in s: s = s.replace(c * k, "") count += 1 if count == 0: break return s ob = Solution() s = "paaappmmmma" k = 3 print(ob.solve(s, k))
Input
"paaappmmmma", 3
Output
ma
- Related Articles
- Program to find string after removing consecutive duplicate characters in Python
- Find all possible substrings after deleting k characters in Python
- Program to find minimum amplitude after deleting K elements in Python
- Python program to find all duplicate characters in a string
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- Program to find minimum length of string after deleting similar ends in Python
- Java Program to find duplicate characters in a String?
- Program to find cost to remove consecutive duplicate characters with costs in C++?
- Java program to find all duplicate characters in a string
- Java Program to Find the Duplicate Characters in a String
- Golang program to find the duplicate characters in the string
- Python – Insert character in each duplicate string after every K elements
- Program to remove duplicate characters from a given string in Python
- Program to find minimum amplitude after deleting KLength sublist in Python

Advertisements