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++
#include <bits/stdc++.h>
using namespace std;
int findFlips( char str[], int n)
{
char last = ' ' ; int res = 0;
for ( int i = 0; i < n; i++) {
if (last != str[i])
res++;
last = str[i];
}
return res / 2;
}
int main()
{
char str[] = "00011110001110" ;
int n = strlen (str);
cout << findFlips(str, n);
return 0;
}
|
Java
public class minFlips {
static int findFlips(String str, int n)
{
char last = ' ' ; int res = 0 ;
for ( int i = 0 ; i < n; i++) {
if (last != str.charAt(i))
res++;
last = str.charAt(i);
}
return res / 2 ;
}
public static void main(String[] args)
{
String str = "00011110001110" ;
int n = str.length();
System.out.println(findFlips(str, n));
}
}
|
Python 3
def findFlips( str , n):
last = ' '
res = 0
for i in range ( n) :
if (last ! = str [i]):
res + = 1
last = str [i]
return res / / 2
if __name__ = = "__main__" :
str = "00011110001110"
n = len ( str )
print (findFlips( str , n))
|
C#
using System;
public class GFG {
static int findFlips(String str, int n)
{
char last = ' ' ; int res = 0;
for ( int i = 0; i < n; i++) {
if (last != str[i])
res++;
last = str[i];
}
return res / 2;
}
public static void Main()
{
String str = "00011110001110" ;
int n = str.Length;
Console.Write(findFlips(str, n));
}
}
|
Javascript
<script>
function findFlips( str , n) {
var last = ' ' ;
var res = 0;
for (i = 0; i < n; i++) {
if (last != str.charAt(i))
res++;
last = str.charAt(i);
}
return parseInt(res / 2);
}
var str = "00011110001110" ;
var n = str.length;
document.write(findFlips(str, n));
</script>
|
PHP
<?php
function findFlips( $str , $n )
{
$last = ' ' ;
$res = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $last != $str [ $i ])
$res ++;
$last = $str [ $i ];
}
return intval ( $res / 2);
}
$str = "00011110001110" ;
$n = strlen ( $str );
echo findFlips( $str , $n );
?>
|
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++;
}
}
else {
countOne++;
while ((i + 1) != n && str[i + 1] == '1' ) {
i++;
}
}
}
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++;
}
} else {
countOne++;
while ((i + 1 ) != n && str.charAt(i + 1 ) == '1' ) {
i++;
}
}
}
int mini = Math.min(countOne, countZero);
System.out.println(mini);
}
}
|
Python3
str = "010101100011"
n = len ( str )
countZero = 0
countOne = 0
i = 0
while i < n:
if str [i] = = '0' :
countZero + = 1
while i + 1 ! = n and str [i + 1 ] = = '0' :
i + = 1
else :
countOne + = 1
while i + 1 ! = n and str [i + 1 ] = = '1' :
i + = 1
i + = 1
mini = min (countOne, countZero)
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++;
while ((i + 1) != n && str[i + 1] == '0' )
{
i++;
}
}
else
{
countOne++;
while ((i + 1) != n && str[i + 1] == '1' )
{
i++;
}
}
}
int mini = Math.Min(countOne, countZero);
Console.WriteLine(mini);
}
}
|
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++;
}
} else {
countOne++;
while (i + 1 < n && str[i +1] === '1' ) {
i++;
}
}
}
const mini = Math.min(countOne, countZero);
console.log(mini);
}
const str = "010101100011" ;
countConsecutive(str);
|
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by nuclode.
Last Updated :
29 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...