Convert given Binary string S to all 1s by changing all 0s to 1s in range [i+1, i+K] if S[i] is 1
Last Updated :
07 Jul, 2021
Given a binary string S of size N and a number K, the task is to find if all the ‘0’s can be changed into ‘1′s in any number of operations. In one operation, if S[i] is initially ‘1’, then all ‘0‘s in the range [i+1, i+K] can be changed to ‘1’s, for 0≤i<N-K.
Examples:
Input: S=”100100″, N=6, K=2
Output: YES
Explanation: S[0] can be used to change S[1] and S[2] into 1s
S[3] can be used to change S[4] and S[5] into 1s
Input: S=”110000″, N=6, K=2
Output: NO
Naive Approach: The simplest approach is to traverse the string in a reverse manner and if ‘0’ is encountered, check if the position of the nearest ‘1’ on the left is more than K indices away. If true, then print “NO”.
Time Complexity: O(N*K)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use a stack. Follow the steps below to solve the problem:
- Initialize two variables flag and count as 1 and 0 respectively to store the result and to count the number of ‘0′s that have been changed by the last occurrence of ‘1′.
- Initialize an empty stack st.
- Traverse the string S, and do the following:
- If stack is empty:
- If the current element is ‘0’, change flag to 0 and break, as this ‘0′ cannot be changed to ‘1’.
- Otherwise, update count to 0 and push 1 to st.
- Otherwise:
- If the current element is ‘0’, do the following:
- Increment count by 1.
- If count becomes equal to K, pop the stack st and update count to 0
- Else, update count to 0.
- If the value of flag is 1, print “YES”, otherwise print “NO” as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void changeCharacters(string S, int N, int K)
{
int flag = 1;
int count = 0;
stack< char > st;
for ( int i = 0; i < N; i++) {
if (st.empty()) {
if (S[i] == '0' ) {
flag = 0;
break ;
}
count = 0;
st.push(S[i]);
}
else {
if (S[i] == '0' ) {
count++;
if (count == K) {
st.pop();
count = 0;
}
}
else {
count = 0;
}
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
int main()
{
string S = "100100" ;
int N = S.length();
int K = 2;
changeCharacters(S, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void changeCharacters(String S, int N, int K)
{
int flag = 1 ;
int count = 0 ;
Stack<Character> st = new Stack<>();
for ( int i = 0 ; i < N; i++)
{
if (st.empty())
{
if (S.charAt(i) == '0' )
{
flag = 0 ;
break ;
}
count = 0 ;
st.push(S.charAt(i));
}
else
{
if (S.charAt(i) == '0' )
{
count++;
if (count == K)
{
st.pop();
count = 0 ;
}
}
else
{
count = 0 ;
}
}
}
if (flag == 1 )
System.out.print( "YES" );
else
System.out.print( "NO" );
}
public static void main(String args[])
{
String S = "100100" ;
int N = S.length();
int K = 2 ;
changeCharacters(S, N, K);
}
}
|
Python3
def changeCharacters(S, N, K):
flag = 1
count = 0
st = []
for i in range (N):
if len (st) = = 0 :
if (S[i] = = '0' ):
flag = 0
break
count = 0
st.append(S[i])
else :
if (S[i] = = '0' ):
count + = 1
if (count = = K):
del st[ - 1 ]
count = 0
else :
count = 0
if (flag):
print ( "YES" )
else :
print ( "NO" )
if __name__ = = '__main__' :
S = "100100"
N = len (S)
K = 2
changeCharacters(S, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void changeCharacters( string S, int N, int K)
{
int flag = 1;
int count = 0;
Stack< char > st = new Stack< char >();
for ( int i = 0; i < N; i++) {
if (st.Count==0) {
if (S[i] == '0' ) {
flag = 0;
break ;
}
count = 0;
st.Push(S[i]);
}
else {
if (S[i] == '0' ) {
count++;
if (count == K) {
st.Pop();
count = 0;
}
}
else {
count = 0;
}
}
}
if (flag == 1)
Console.Write( "YES" );
else
Console.Write( "NO" );
}
public static void Main()
{
string S = "100100" ;
int N = S.Length;
int K = 2;
changeCharacters(S, N, K);
}
}
|
Javascript
<script>
function changeCharacters(S, N, K) {
let flag = 1;
let count = 0;
let st = new Array();
for (let i = 0; i < N; i++) {
if (st.length == 0) {
if (S[i] == '0' ) {
flag = 0;
break ;
}
count = 0;
st.push(S[i]);
}
else {
if (S[i] == '0' ) {
count++;
if (count == K) {
st.pop();
count = 0;
}
}
else {
count = 0;
}
}
}
if (flag == 1)
document.write( "YES" );
else
document.write( "NO" );
}
let S = "100100" ;
let N = S.Length;
let K = 2;
changeCharacters(S, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) since at most one character is present in the stack at any moment
Share your thoughts in the comments
Please Login to comment...