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; } |
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 |
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 |
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 |
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.