Find Binary string by converting all 01 or 10 to 11 after M iterations
Last Updated :
08 Sep, 2021
Given a binary string str[] of size N and an integer M. This binary string can be modified by flipping all the 0’s to 1 which have exactly one 1 as a neighbour. The task is to find the final state of the binary string after M such iterations.
Note: 2?N?103, 1?M?109
Examples:
Input: str=”01100″, M=1
Output: 11110
Explanation: After First Iteration: 11110
Input: str = “0110100”, M=3
Output: 1110111
Explanation: After First Iteration: 1110110, After Second Iteration: 1110111, After Third Iteration: Remains the same.
Approach: The solution is based on the observation that the modification can go on for no more than N iterations, because even if in each iteration, atleast one 0 is flipped, then it would go on for maximum N times and, if no zero is flipped in an iteration, then this would mean that the binary string remain in the same state as on the previous step and the simulation is over. Hence, the total number of iterations will be a minimum of N and M. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findString(string str, int M)
{
int N = str.length();
M = min(M, N);
string s1 = "" ;
while (M != 0) {
s1 = "" ;
for ( int i = 0; i < N; i++) {
if (str[i] == '0' ) {
if ((str[i - 1] == '1'
&& str[i + 1] != '1' )
|| (str[i - 1] != '1'
&& str[i + 1] == '1' ))
s1 += '1' ;
else
s1 += '0' ;
}
else
s1 += '1' ;
}
if (str == s1)
break ;
str = s1;
M--;
}
cout << s1;
}
int main()
{
string str = "0110100" ;
int M = 3;
findString(str, M);
return 0;
}
|
Java
import java.io.*;
import static java.lang.Math.min;
import java.lang.*;
class GFG
{
public static void findString(String str, int M)
{
int N = str.length();
M = Math.min(M, N);
String s1 = "" ;
while (M != 0 ) {
s1 = "" ;
for ( int i = 0 ; i < N; i++) {
if (str.charAt(i) == '0' ) {
if ((str.charAt(i) == '1'
&& str.charAt(i) != '1' )
|| (str.charAt(i) == '1'
&& str.charAt(i) == '1' ))
s1 += '1' ;
else
s1 += '0' ;
}
else
s1 += '1' ;
}
if (str == s1)
break ;
str = s1;
M--;
}
System.out.print(s1);
}
public static void main (String[] args)
{
String str = "0110100" ;
int M = 3 ;
findString(str, M);
}
}
|
Python3
def findString( str ,M):
N = len ( str )
M = min (M, N)
s1 = ""
while (M ! = 0 ):
s1 = ""
for i in range (N - 1 ):
if ( str [i] = = '0' ):
if (( str [i - 1 ] = = '1' and str [i + 1 ] ! = '1' ) or ( str [i - 1 ] ! = '1' and str [i + 1 ] = = '1' )):
s1 + = '1'
else :
s1 + = '0'
else :
s1 + = '1'
if ( str = = s1):
break
s1 + = '1'
str = s1
M - = 1
print (s1)
if __name__ = = '__main__' :
str = "0110100"
M = 3
findString( str , M)
|
C#
using System;
class GFG {
static void findString( string str, int M)
{
int N = str.Length;
M = Math.Min(M, N);
string s1 = "" ;
while (M != 0) {
s1 = "" ;
for ( int i = 0; i < N; i++) {
if (str[i] == '0' )
{
if (((i>0 && str[i - 1] == '1' ) && (i<N-1 && str[i + 1] != '1' )) || ((i>0 && str[i - 1] != '1' ) && (i<N-1 && str[i + 1] == '1' )))
{
s1 += '1' ;
}
else
{
if (i==0 || i==N-1)
{
s1 += '1' ;
}
else
{
s1 += '0' ;
}
}
}
else
{
s1 += '1' ;
}
}
if (str == s1)
break ;
M--;
}
Console.WriteLine(s1);
}
static void Main() {
string str = "0110100" ;
int M = 3;
findString(str, M);
}
}
|
Javascript
<script>
function findlet(str, M)
{
let N = str.length;
M = Math.min(M, N);
let s1 = "" ;
while (M != 0) {
s1 = "" ;
for (let i = 0; i < N; i++) {
if (str[i] == '0' ) {
if ((str[i - 1] == '1'
&& str[i + 1] != '1' )
|| (str[i - 1] != '1'
&& str[i + 1] == '1' ))
s1 += '1' ;
else
s1 += '0' ;
}
else
s1 += '1' ;
}
if (str == s1)
break ;
str = s1;
M--;
}
document.write(s1);
}
let str = "0110100" ;
let M = 3;
findlet(str, M);
</script>
|
Time Complexity: O(min(M, N)*N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...