Given a binary string S of even size that contains only 0s and 1s, the task is to find the minimum flips (that is 0 to 1 or 1 to 0) such that every contiguous subsegment that contains the same bit is of even size.
Examples:
Input: S = “1110011000 “
Output: 3
Explanation: Change S[2], S[5] and S[6] to ‘0’.
After that S becomes “1100000000”, it can be divided into
“11” and “00000000”, which have lengths 2 and 8 respectively.
There are other ways to operate 3 times such as
“1111110000”, “1100001100”, “1111001100”.
Input: 100110
Output: 3
Explanation: The given string can be converted into 000000
with 3 operations or 111111.
Approach: To solve the problem follow the below observations:
It can be said that we have to divide the string into many adjacent binaries with the length of 2 where the strings will be either 00 or 11. So, we have to only take care of sequence where s[i] != s[i+1]
- Now in order to find a minimum operation sequence like “1011” should be converted to “1111” and “0100” should be converted to “0000”.
- So, the minimum number of operations is 1. Hence once we make s[i] = s[i + 1] we will move i to i+2 to check whether they are equal or not.
Follow the steps mentioned below to implement the idea:
- Iterate from i = 0 to N-1:
- Check if S[i] is the same as S[i+1].
- If they are the same then no bit flip is required.
- Otherwise, increment the number of flips by 1.
- After each check move from i to i+2.
- The total number of flips calculated is the required minimum answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int solve(string s)
{
int ans = 0;
int n = s.size();
for ( int i = 0; i < n; i += 2) {
if (s[i] != s[i + 1])
ans++;
}
return ans;
}
int main()
{
string S = "1110011000" ;
cout << solve(S);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int solve(String s)
{
int ans = 0 ;
int n = s.length();
for ( int i = 0 ; i < n; i += 2 ) {
if (s.charAt(i) != s.charAt(i+ 1 ))
ans++;
}
return ans;
}
public static void main(String[] args)
{
String S = "1110011000" ;
System.out.print(solve(S));
}
}
|
Python3
def solve(s):
ans = 0
n = len (s)
for i in range ( 0 , n, + 2 ):
if (s[i] is not s[i + 1 ]):
ans + = 1
return ans
S = "1110011000"
print (solve(S))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int solve( string s)
{
int ans = 0;
int n = s.Length;
for ( int i = 0; i < n; i += 2) {
if (s[i] != s[i+1])
ans++;
}
return ans;
}
public static void Main()
{
string S = "1110011000" ;
Console.WriteLine(solve(S));
}
}
|
Javascript
<script>
function solve(s) {
let ans = 0;
let n = s.length;
for (let i = 0; i < n; i += 2) {
if (s[i] != s[i + 1])
ans++;
}
return ans;
}
let S = "1110011000" ;
document.write(solve(S));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Another Approach:
- We start by defining a function solve that takes a string s as input and returns an integer.
- We then initialize the variables n, count, and flips. n is the length of the string s, count is the length of the current segment, and flips is the number of flips needed.
- We then loop through the string s starting from the second character (i=1) to the end (i<n).
- Inside the loop, we check if the current character s[i] is different from the previous character s[i-1]. If it is different, we have a new segment and we increment the count variable.
- If the current character s[i] is the same as the previous character s[i-1], we check if the length of the current segment count is odd. If it is odd, we need to flip one bit in that segment to make it even, so we increment the flips variable.
- We then reset the count variable to 1 for the next segment.
- After the loop, we need to check the length of the last segment, which may not have been accounted for in the loop. If it is odd, we need to flip one bit in that segment to make it even, so we increment the flips variable again.
- Finally, we return the total number of flips needed.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int solve(string s) {
int n = s.size();
int count = 0, flips = 0;
for ( int i = 1; i < n; i++) {
if (s[i] != s[i-1]) {
count++;
} else {
if (count % 2 == 1) {
flips++;
}
count = 1;
}
}
if (count % 2 == 1) {
flips++;
}
return flips;
}
int main()
{
string S = "1110011000" ;
cout << solve(S);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int solve(String s) {
int n = s.length();
int count = 0 , flips = 0 ;
for ( int i = 1 ; i < n; i++) {
if (s.charAt(i) != s.charAt(i- 1 )) {
count++;
} else {
if (count % 2 == 1 ) {
flips++;
}
count = 1 ;
}
}
if (count % 2 == 1 ) {
flips++;
}
return flips;
}
public static void main(String[] args) {
String S = "1110011000" ;
System.out.println(solve(S));
}
}
|
Python3
def solve(s):
n = len (s)
count = 0
flips = 0
for i in range ( 1 , n):
if s[i] ! = s[i - 1 ]:
count + = 1
else :
if count % 2 = = 1 :
flips + = 1
count = 1
if count % 2 = = 1 :
flips + = 1
return flips
S = "1110011000"
print (solve(S))
|
C#
using System;
namespace CodeTranslation
{
class Program
{
static int Solve( string s)
{
int n = s.Length;
int count = 0, flips = 0;
for ( int i = 1; i < n; i++)
{
if (s[i] != s[i - 1])
{
count++;
}
else
{
if (count % 2 == 1)
{
flips++;
}
count = 1;
}
}
if (count % 2 == 1)
{
flips++;
}
return flips;
}
static void Main( string [] args)
{
string S = "1110011000" ;
Console.WriteLine(Solve(S));
}
}
}
|
Javascript
function solve(s) {
let n = s.length;
let count = 0,
flips = 0;
for (let i = 1; i < n; i++) {
if (s[i] != s[i - 1]) {
count++;
} else {
if (count % 2 == 1) {
flips++;
}
count = 1;
}
}
if (count % 2 == 1) {
flips++;
}
return flips;
}
let S = "1110011000" ;
console.log(solve(S));
|
Time complexity: O(N)
Auxiliary Space: O(1)