Min flips of continuous characters to make all characters same in a string
Given a string consisting only of 1’s and 0’s. In one flip we can change any continuous sequence of this string. Find this minimum number of flips so the string consist of same characters only.
Examples:
Input : 00011110001110
Output : 2
We need to convert 1's sequence
so string consist of all 0's.
Input : 010101100011
Output : 4
Method 1 (Change in value encountered)
We need to find the min flips in string so all characters are equal. All we have to find numbers of sequence which consisting of 0’s or 1’s only. Then number of flips required will be half of this number as we can change all 0’s or all 1’s.
C++
// CPP program to find min flips in binary // string to make all characters equal #include <bits/stdc++.h> using namespace std; // To find min number of flips in binary string int findFlips( char str[], int n) { char last = ' ' ; int res = 0; for ( int i = 0; i < n; i++) { // If last character is not equal // to str[i] increase res if (last != str[i]) res++; last = str[i]; } // To return min flips return res / 2; } // Driver program to check findFlips() int main() { char str[] = "00011110001110" ; int n = strlen (str); cout << findFlips(str, n); return 0; } |
Java
// Java program to find min flips in binary // string to make all characters equal public class minFlips { // To find min number of flips in binary string static int findFlips(String str, int n) { char last = ' ' ; int res = 0 ; for ( int i = 0 ; i < n; i++) { // If last character is not equal // to str[i] increase res if (last != str.charAt(i)) res++; last = str.charAt(i); } // To return min flips return res / 2 ; } // Driver program to check findFlips() public static void main(String[] args) { String str = "00011110001110" ; int n = str.length(); System.out.println(findFlips(str, n)); } } |
Python 3
# Python 3 program to find min flips in # binary string to make all characters equal # To find min number of flips in # binary string def findFlips( str , n): last = ' ' res = 0 for i in range ( n) : # If last character is not equal # to str[i] increase res if (last ! = str [i]): res + = 1 last = str [i] # To return min flips return res / / 2 # Driver Code if __name__ = = "__main__" : str = "00011110001110" n = len ( str ) print (findFlips( str , n)) # This code is contributed by ita_c |
C#
// C# program to find min flips in // binary string to make all // characters equal using System; public class GFG { // To find min number of flips // in binary string static int findFlips(String str, int n) { char last = ' ' ; int res = 0; for ( int i = 0; i < n; i++) { // If last character is not // equal to str[i] increase // res if (last != str[i]) res++; last = str[i]; } // To return min flips return res / 2; } // Driver program to check findFlips() public static void Main() { String str = "00011110001110" ; int n = str.Length; Console.Write(findFlips(str, n)); } } // This code is contributed by nitin mittal |
Javascript
<script> // JavaScript program to find min flips in binary // string to make all characters equal // To find min number of flips in binary string function findFlips( str , n) { var last = ' ' ; var res = 0; for (i = 0; i < n; i++) { // If last character is not equal // to str[i] increase res if (last != str.charAt(i)) res++; last = str.charAt(i); } // To return min flips return parseInt(res / 2); } // Driver program to check findFlips() var str = "00011110001110" ; var n = str.length; document.write(findFlips(str, n)); // This code contributed by aashish1995 </script> |
PHP
<?php // PHP program to find min flips in binary // string to make all characters equal // To find min number of // flips in binary string function findFlips( $str , $n ) { $last = ' ' ; $res = 0; for ( $i = 0; $i < $n ; $i ++) { // If last character is not equal // to str[i] increase res if ( $last != $str [ $i ]) $res ++; $last = $str [ $i ]; } // To return min flips return intval ( $res / 2); } // Driver Code $str = "00011110001110" ; $n = strlen ( $str ); echo findFlips( $str , $n ); // This code is contributed by Sam007 ?> |
2
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2(Count continuous 0 and 1 )
We can count the number of continuous 0 and continuous 1.
Since we have to take minimum, we use min function to take value with less continuous number. And output it.
Procedure:- Take two variable to count continuous 0 and 1. If 0 is encountered increment countZero and skip 0’s in continuation. Do same with 1 and its continuation. In the end the variables with less value is sent as output
C++
#include <bits/stdc++.h> #include <iostream> using namespace std; int main() { string str = "010101100011" ; int n = str.size(); int countZero = 0; int countOne = 0; for ( int i = 0; i < n; i++) { if (str[i] == '0' ) { countZero++; while ((i + 1) != n && str[i + 1] == '0' ) { i++; // cout<<"Skip 0"; } } else { countOne++; while ((i + 1) != n && str[i + 1] == '1' ) { i++; // cout<<"Skip 1"; } } } int mini = min(countOne, countZero); cout << mini << endl; return 0; } |
Java
public class Main { public static void main(String[] args) { String str = "010101100011" ; int n = str.length(); int countZero = 0 ; int countOne = 0 ; for ( int i = 0 ; i < n; i++) { if (str.charAt(i) == '0' ) { countZero++; while ((i + 1 ) != n && str.charAt(i + 1 ) == '0' ) { i++; // System.out.println("Skip 0"); } } else { countOne++; while ((i + 1 ) != n && str.charAt(i + 1 ) == '1' ) { i++; // System.out.println("Skip 1"); } } } int mini = Math.min(countOne, countZero); System.out.println(mini); } } |
Python3
str = "010101100011" n = len ( str ) countZero = 0 # Counter for consecutive '0's countOne = 0 # Counter for consecutive '1's i = 0 # Index for string traversal while i < n: # Check for consecutive '0's if str [i] = = '0' : countZero + = 1 # Count the consecutive '0's while i + 1 ! = n and str [i + 1 ] = = '0' : i + = 1 # Move to the next character # print("Skip 0") # Optional: Uncomment to display skipping '0's # Check for consecutive '1's else : countOne + = 1 # Count the consecutive '1's while i + 1 ! = n and str [i + 1 ] = = '1' : i + = 1 # Move to the next character # print("Skip 1") # Optional: Uncomment to display skipping '1's i + = 1 # Move to the next character after consecutive count # Calculate the minimum count between consecutive '0's and '1's mini = min (countOne, countZero) # Print the minimum count print (mini) |
C#
using System; class Program { static void Main() { string str = "010101100011" ; int n = str.Length; int countZero = 0; int countOne = 0; for ( int i = 0; i < n; i++) { if (str[i] == '0' ) { countZero++; // Increment the count of '0's while ((i + 1) != n && str[i + 1] == '0' ) { i++; // Skip consecutive '0's // Console.WriteLine("Skip 0"); } } else { countOne++; // Increment the count of '1's while ((i + 1) != n && str[i + 1] == '1' ) { i++; // Skip consecutive '1's // Console.WriteLine("Skip 1"); } } } int mini = Math.Min(countOne, countZero); // Find the minimum count of '0's and '1's Console.WriteLine(mini); // Output the result } } |
Javascript
function countConsecutive(str) { const n = str.length; let countZero = 0; let countOne = 0; for (let i = 0; i < n; i++) { if (str[i] === '0' ) { countZero++; while (i + 1 < n && str[i + 1] === '0' ) { i++; // console.log("Skip 0"); } } else { countOne++; while (i + 1 < n && str[i +1] === '1' ) { i++; // console.log("Skip 1"); } } } const mini = Math.min(countOne, countZero); console.log(mini); } const str = "010101100011" ; countConsecutive(str); |
4
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by nuclode.
Contact Us