Minimum letters to be removed to make all occurrences of a given letter continuous

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:



  1. Find the first occurrence of the character K.
  2. Find the last occurrence of the character K.
  3. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.