Minimize insertions required to make all characters of a given string equal
Given a binary string S of length N, the task is to find the minimum number of characters required to be inserted such that all the characters in the string becomes the same based on the condition that:
If β1β is inserted into the string, then all the β0βs nearest to the inserted β1β is flipped or vice-versa.
Examples:
Input: S = β11100β
Output: 1
Explanation:
Operation 1: Inserting β1β at the last of the given string modifies S to β111001β. Adding β1β to the last flips all the nearest β0βs to the inserted β1β. Therefore, the resultant string is β111111β.
After completing the above operation, all the characters of the string are the same. Therefore count of operations is 1.Input: S = β0101010101β
Output: 9
Approach: The idea is to solve this problem by Greedy Approach based on the following observations:
- It can be seen that inverting one continuous section of β1βs or β0βs reduces the number of sections by one in this operation. Therefore, it is sufficient to repeat this operation to make it all into one section. The number of operations required is equal to sections β 1.
- In simpler terms, count the total number of non-equal adjacent pair of characters, so that inverting one of them can convert the whole substring into similar substrings.
Follow the steps below to solve the problem:
- Initialize a variable, say count, that stores the count of different adjacent characters.
- Traverse the string and check if the current and the next characters are different, then increment the value of count.
- After completing the above steps, print the value of count as the minimum required operations.
Below is the implementation of the above approach:
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate the minimum // number of operations required to make // all characters of the string same int minOperations(string& S) { // Stores count of operations int count = 0; // Traverse the string for ( int i = 1; i < S.length(); i++) { // Check if adjacent // characters are same or not if (S[i] != S[i - 1]) { // Increment count count += 1; } } // Print the count obtained cout << count; } // Driver Code int main() { string S = "0101010101" ; minOperations(S); return 0; } |
Java
// Java program to implement // the above approach import java.io.*; import java.util.*; class GFG { // Function to calculate the minimum // number of operations required to make // all characters of the string same static void minOperations(String S) { // Stores count of operations int count = 0 ; // Traverse the string for ( int i = 1 ; i < S.length(); i++) { // Check if adjacent // characters are same or not if (S.charAt(i) != S.charAt(i - 1 )) { // Increment count count += 1 ; } } // Print the count obtained System.out.print(count); } // Driver Code public static void main(String[] args) { String S = "0101010101" ; minOperations(S); } } // This code is contributed by susmitakundugoaldanga. |
Python3
# Python program to implement # the above approach # Function to calculate the minimum # number of operations required to make # all characters of the string same def minOperations(S): # Stores count of operations count = 0 ; # Traverse the string for i in range ( 1 , len (S)): # Check if adjacent # characters are same or not if (S[i] ! = S[i - 1 ]): # Increment count count + = 1 ; # Print count obtained print (count); # Driver Code if __name__ = = '__main__' : S = "0101010101" ; minOperations(S); # This code is contributed by 29AjayKumar |
C#
// C# program to implement // the above approach using System; using System.Collections.Generic; class GFG { // Function to calculate the minimum // number of operations required to make // all characters of the string same static void minOperations( string S) { // Stores count of operations int count = 0; // Traverse the string for ( int i = 1; i < S.Length; i++) { // Check if adjacent // characters are same or not if (S[i] != S[i - 1]) { // Increment count count += 1; } } // Print the count obtained Console.Write(count); } // Driver Code public static void Main() { string S = "0101010101" ; minOperations(S); } } // This code is contributed by code_hunt. |
Javascript
<script> // JavaScript program for the above approach // Function to calculate the minimum // number of operations required to make // all characters of the string same function minOperations(S) { // Stores count of operations var count = 0; // Traverse the string for ( var i = 1; i < S.length; i++) { // Check if adjacent // characters are same or not if (S[i] !== S[i - 1]) { // Increment count count += 1; } } // Print the count obtained document.write(count); } // Driver Code var S = "0101010101" ; minOperations(S); </script> |
9
Time Complexity: O(N)
Auxiliary Space: O(1)
Contact Us