Given a string str and a character K, the task is to find the minimum number of elements that should be removed from this sequence so that all occurrences of the given character K become continuous.
Input: str = “ababababa”, K = ‘a’
All the occurances of the character ‘b’ should be removed in order to make the occurances of ‘a’ continuous.
Input: str = “kprkkoinkopt”, K = ‘k’
Approach: The idea is to find the number of characters other than K in between the first and last occurrence of the given character K. In order to do this, the following steps are followed:
- Find the first occurrence of the character K.
- Find the last occurrence of the character K.
- Iterate between the indices and find the number of characters present in between those indices other than K. This is the required answer.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
- Check if the string contains consecutive letters and each letter occurs exactly once
- Minimum digits to be removed to make either all digits or alternating digits same
- Minimum number of letters needed to make a total of n
- Minimum size substring to be removed to make a given string palindromic
- Min flips of continuous characters to make all characters same in a string
- Minimum number of characters to be removed to make a binary string alternate
- Make palindromic string non-palindromic by rearranging its letters
- Wrap every continuous instance of given word with some given string
- Move all occurence of letter 'x' from the string s to the end using Recursion
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- String containing first letter of every word in a given string with spaces
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Put spaces between words starting with capital letters
- Regex in Python to put spaces between words starting with capital letters
- Program to print reciprocal of letters
- Smallest Greater (than S) String of length K whose letters are subset of S
- Length of the longest substring with no consecutive same letters
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.