Flip all 0s in given Binary Strings K times with different neighbours
Last Updated :
14 Jul, 2022
Given a binary string S of size N and a positive integer K, the task is to repeatedly modify the given string K number of times by flipping all the 0s having different adjacent characters in each iteration.
Note: For the character 0 present at 0th index then it will change to 1 only when 1st index character is 1 and if the last index character is 0 then it changes to ‘1’ if the second last index character is ‘1’.
Examples:
Input: S = “01001”, K = 2
Output: 11111
Explanation:
Below are the operation performed K(= 2) number of times:|
Operation 1: After flipping the string at indices 0, 2, 3, the string modifies to “11111”.
Operation 2: There is no such possible flipping for the modified string S = “11111”.
After the above operations, the modified string is “11111”.
Input: S = “10010001”, K = 3
Output: 11111011
Approach: The given problem can be solved by performing the given operation K number of times and then print the resultant string formed. Follow the steps below to solve this problem:
- Iterate over the range [0, K – 1] using the variable i and perform the following steps:
- Traverse the given string S over the range [0, N – 1] using the variable j and perform the following steps:
- If the character at 0th index is 0, then replace this value with the first index value.
- Otherwise, if character 0 is present at the last index then replace this value with the second last index character.
- Otherwise, convert all the 0s to 1 if the adjacent characters are different.
- After the above steps, if the string remains the same before the modification, then break out of the loop.
- After completing the above steps, print the string S as the resultant modified string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void convertString(string S, int k)
{
int n = S.length();
string temp = S;
for ( int i = 0; i < k; i++) {
for ( int j = 0; j < n; j++) {
if (j == 0 && S[j] == '0' ) {
temp[j] = S[j + 1];
}
else if (j == n - 1
&& S[j] == '0' ) {
temp[j] = S[j - 1];
}
else if (S[j - 1] != S[j + 1]
&& S[j] == '0' ) {
temp[j] = '1' ;
}
}
if (S == temp) {
break ;
}
S = temp;
}
cout << S;
}
int main()
{
string S = "10010001" ;
int K = 1;
convertString(S, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static void convertString(String Str, int k)
{
int n = Str.length();
char [] S = Str.toCharArray();
char [] temp = Str.toCharArray();
for ( int i = 0 ; i < k; i++) {
for ( int j = 0 ; j < n; j++) {
if (j == 0 && S[j] == '0' ) {
temp[j] = S[j + 1 ];
}
else if (j == n - 1 && S[j] == '0' ) {
temp[j] = S[j - 1 ];
}
else if (j<n- 1 && j> 0 )
{
if (S[j - 1 ] != S[j + 1 ] && S[j] == '0' ) {
temp[j] = '1' ;
}
}
}
if (S == temp) {
break ;
}
S = temp;
}
System.out.println(S);
}
public static void main(String[] args)
{
String S = "10010001" ;
int K = 1 ;
convertString(S, K);
}
}
|
Python3
def convertString(S, k):
n = len (S)
temp = S
temp = list (temp)
for i in range (k):
for j in range (n - 1 ):
if (j = = 0 and S[j] = = '0' ):
temp[j] = S[j + 1 ]
elif (j = = n - 1 and S[j] = = '0' ):
temp[j] = S[j - 1 ]
elif (S[j - 1 ] ! = S[j + 1 ] and S[j] = = '0' ):
temp[j] = '1'
if (S = = temp):
break
temp = ''.join(temp)
S = temp
print (S)
if __name__ = = '__main__' :
S = "10010001"
K = 1
convertString(S, K)
|
C#
using System;
using System.Text;
public class GFG {
static void convertString( string S, int k)
{
int n = S.Length;
StringBuilder temp = new StringBuilder(S);
for ( int i = 0; i < k; i++) {
for ( int j = 0; j < n; j++) {
if (j == 0 && S[j] == '0' ) {
temp[j] = S[j + 1];
}
else if (j == n - 1 && S[j] == '0' ) {
temp[j] = S[j - 1];
}
else if (j>0 && j<n-1 && S[j - 1] != S[j + 1] && S[j] == '0' ) {
temp[j] = '1' ;
}
}
string t = temp.ToString();
if (S == t) {
break ;
}
S = t;
}
Console.Write(S);
}
public static void Main()
{
string S = "10010001" ;
int K = 1;
convertString(S, K);
}
}
|
Javascript
<script>
function convertString(S, k)
{
let n = S.length;
let temp = S;
for (let i = 0; i < k; i++) {
for (let j = 0; j < n; j++) {
if (j == 0 && S[j] == '0' ) {
temp[j] = S[j + 1];
}
else if (j == n - 1
&& S[j] == '0' ) {
temp[j] = S[j - 1];
}
else if (S[j - 1] != S[j + 1]
&& S[j] == '0' ) {
temp = temp.substring(0,j)+ '1' +temp.substring(j+1);
}
}
if (S == temp) {
break ;
}
S = temp;
}
document.write(S);
}
let S = "10010001" ;
let K = 1;
convertString(S, K);
</script>
|
Output : 11111011
Time Complexity: O(N*K)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...