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.

**Examples:**

Input:str = “ababababa”, K = ‘a’

Output:4

Explanation:

All the occurances of the character ‘b’ should be removed in order to make the occurances of ‘a’ continuous.

Input:str = “kprkkoinkopt”, K = ‘k’

Output:5

**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:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum number of ` `// deletions required to make the occurrences ` `// of the given character K continuous ` `int` `noOfDeletions(string str, ` `char` `k) ` `{ ` ` ` `int` `ans = 0, cnt = 0, pos = 0; ` ` ` ` ` `// Find the first occurrence of the given letter ` ` ` `while` `(pos < str.length() && str[pos] != k) { ` ` ` `pos++; ` ` ` `} ` ` ` ` ` `int` `i = pos; ` ` ` ` ` `// Iterate from the first occurrence ` ` ` `// till the end of the sequence ` ` ` `while` `(i < str.length()) { ` ` ` ` ` `// Find the index from where the occurrence ` ` ` `// of the character is not continuous ` ` ` `while` `(i < str.length() && str[i] == k) { ` ` ` `i = i + 1; ` ` ` `} ` ` ` ` ` `// Update the answer with the number of ` ` ` `// elements between non-consecutive occurrences ` ` ` `// of the given letter ` ` ` `ans = ans + cnt; ` ` ` `cnt = 0; ` ` ` `while` `(i < str.length() && str[i] != k) { ` ` ` `i = i + 1; ` ` ` ` ` `// Update the count for all letters ` ` ` `// which are not equal to the given letter ` ` ` `cnt = cnt + 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str1 = ` `"ababababa"` `; ` ` ` `char` `k1 = ` `'a'` `; ` ` ` `// Calling the function ` ` ` `cout << noOfDeletions(str1, k1) << endl; ` ` ` ` ` `string str2 = ` `"kprkkoinkopt"` `; ` ` ` `char` `k2 = ` `'k'` `; ` ` ` `// Calling the function ` ` ` `cout << noOfDeletions(str2, k2) << endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the minimum number of ` `// deletions required to make the occurrences ` `// of the given character K continuous ` `static` `int` `noOfDeletions(String str, ` `char` `k) ` `{ ` ` ` `int` `ans = ` `0` `, cnt = ` `0` `, pos = ` `0` `; ` ` ` ` ` `// Find the first occurrence of the given letter ` ` ` `while` `(pos < str.length() && str.charAt(pos) != k) { ` ` ` `pos++; ` ` ` `} ` ` ` ` ` `int` `i = pos; ` ` ` ` ` `// Iterate from the first occurrence ` ` ` `// till the end of the sequence ` ` ` `while` `(i < str.length()) { ` ` ` ` ` `// Find the index from where the occurrence ` ` ` `// of the character is not continuous ` ` ` `while` `(i < str.length() && str.charAt(i) == k) { ` ` ` `i = i + ` `1` `; ` ` ` `} ` ` ` ` ` `// Update the answer with the number of ` ` ` `// elements between non-consecutive occurrences ` ` ` `// of the given letter ` ` ` `ans = ans + cnt; ` ` ` `cnt = ` `0` `; ` ` ` `while` `(i < str.length() && str.charAt(i) != k) { ` ` ` `i = i + ` `1` `; ` ` ` ` ` `// Update the count for all letters ` ` ` `// which are not equal to the given letter ` ` ` `cnt = cnt + ` `1` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str1 = ` `"ababababa"` `; ` ` ` `char` `k1 = ` `'a'` `; ` ` ` ` ` `// Calling the function ` ` ` `System.out.print(noOfDeletions(str1, k1) +` `"\n"` `); ` ` ` ` ` `String str2 = ` `"kprkkoinkopt"` `; ` ` ` `char` `k2 = ` `'k'` `; ` ` ` ` ` `// Calling the function ` ` ` `System.out.print(noOfDeletions(str2, k2) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function to find the minimum number of ` `# deletions required to make the occurrences ` `# of the given character K continuous ` `def` `noOfDeletions(string, k) : ` ` ` ` ` `ans ` `=` `0` `; cnt ` `=` `0` `; pos ` `=` `0` `; ` ` ` ` ` `# Find the first occurrence of the given letter ` ` ` `while` `(pos < ` `len` `(string) ` `and` `string[pos] !` `=` `k) : ` ` ` `pos ` `+` `=` `1` `; ` ` ` ` ` `i ` `=` `pos; ` ` ` ` ` `# Iterate from the first occurrence ` ` ` `# till the end of the sequence ` ` ` `while` `(i < ` `len` `(string)) : ` ` ` ` ` `# Find the index from where the occurrence ` ` ` `# of the character is not continuous ` ` ` `while` `(i < ` `len` `(string) ` `and` `string[i] ` `=` `=` `k) : ` ` ` `i ` `=` `i ` `+` `1` `; ` ` ` ` ` `# Update the answer with the number of ` ` ` `# elements between non-consecutive occurrences ` ` ` `# of the given letter ` ` ` `ans ` `=` `ans ` `+` `cnt; ` ` ` `cnt ` `=` `0` `; ` ` ` `while` `(i < ` `len` `(string) ` `and` `string[i] !` `=` `k) : ` ` ` `i ` `=` `i ` `+` `1` `; ` ` ` ` ` `# Update the count for all letters ` ` ` `# which are not equal to the given letter ` ` ` `cnt ` `=` `cnt ` `+` `1` `; ` ` ` ` ` `# Return the count ` ` ` `return` `ans; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `str1 ` `=` `"ababababa"` `; ` ` ` `k1 ` `=` `'a'` `; ` ` ` ` ` `# Calling the function ` ` ` `print` `(noOfDeletions(str1, k1)); ` ` ` ` ` `str2 ` `=` `"kprkkoinkopt"` `; ` ` ` `k2 ` `=` `'k'` `; ` ` ` ` ` `# Calling the function ` ` ` `print` `(noOfDeletions(str2, k2)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the minimum number of ` `// deletions required to make the occurrences ` `// of the given character K continuous ` `static` `int` `noOfDeletions(String str, ` `char` `k) ` `{ ` ` ` `int` `ans = 0, cnt = 0, pos = 0; ` ` ` ` ` `// Find the first occurrence of the given letter ` ` ` `while` `(pos < str.Length && str[pos] != k) { ` ` ` `pos++; ` ` ` `} ` ` ` ` ` `int` `i = pos; ` ` ` ` ` `// Iterate from the first occurrence ` ` ` `// till the end of the sequence ` ` ` `while` `(i < str.Length) { ` ` ` ` ` `// Find the index from where the occurrence ` ` ` `// of the character is not continuous ` ` ` `while` `(i < str.Length && str[i] == k) { ` ` ` `i = i + 1; ` ` ` `} ` ` ` ` ` `// Update the answer with the number of ` ` ` `// elements between non-consecutive occurrences ` ` ` `// of the given letter ` ` ` `ans = ans + cnt; ` ` ` `cnt = 0; ` ` ` `while` `(i < str.Length && str[i] != k) { ` ` ` `i = i + 1; ` ` ` ` ` `// Update the count for all letters ` ` ` `// which are not equal to the given letter ` ` ` `cnt = cnt + 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String str1 = ` `"ababababa"` `; ` ` ` `char` `k1 = ` `'a'` `; ` ` ` ` ` `// Calling the function ` ` ` `Console.Write(noOfDeletions(str1, k1) +` `"\n"` `); ` ` ` ` ` `String str2 = ` `"kprkkoinkopt"` `; ` ` ` `char` `k2 = ` `'k'` `; ` ` ` ` ` `// Calling the function ` ` ` `Console.Write(noOfDeletions(str2, k2) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

4 5

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.

## Recommended Posts:

- 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 contribute@geeksforgeeks.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.