Replace ‘?’ in string with zero or one
Last Updated :
02 Aug, 2023
Given a binary string consisting of 0s and 1s and ‘?’ characters and an integer K, it is allowed to replace ‘?’ with 0 or 1, the task is to check if there is a unique way of replacing ‘?’ with 0s and 1s such that there is only 1 substring of K length containing all 1s. If there is only one way to do so print “Yes” otherwise “No” in all other cases.
Note: There should be only 1 possible position of K length substring of all 1s for the answer to be Yes.
Examples:
Input: S = ?1?0, K = 2
Output: No
Explanation: There are two possible ways which satisfies all the conditons, S = 1100, 0110. Hence due to multiple ways the output would be No.
Input: S = 00?1???10?, K = 5
Output: Yes
Explanation: There is only one possible string S = 0001111100 that have exactly K length substring of all 1s and is the only possible way.
Naive Approach: The basic way to solve the problem is as follows:
For every K-length substring, we can make that all 1s only if all characters in the substring are ‘1’s or ‘?’s and all other characters outside that substring are all ‘0’s or ‘?’s.
We can check this with brute force for every K-length substring and count the respective number of characters.
Time complexity: O(|s|K)
Auxiliary Space: O(1)
Efficient-Approach: To solve the problem follow the below idea:
Moving from Si to Si+K we can keep track of the changes. Now keep count of the number of valid ways. If there is only 1 way, then print “Yes” or else “No”.
Steps that were to follow the above approach:
- Make two prefix arrays sum and tot where sum[i] stores the number of 0s up to index i and tot[i] stores the number of 1s up to index i respectively in string S.
- Now iterate in the range [k, n] where n is the length of string s.
- To check if we can make a valid substring, we have to check three conditions: (tot[i – k] == 0 and tot[n] – tot[i] == 0 and sum[i] – sum[i – k] == 0)
- Initialize a variable cnt = 0, to store the number of valid substrings.
- Whenever we find these three conditions true for a substring increment cnt = cnt + 1.
- Check if cnt == 1 at the end. If cnt == 1, then print “Yes” or else “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string isValidString(string s, int k)
{
int n = s.size();
vector< int > sum(n + 1, 0);
vector< int > tot(n + 1, 0);
for ( int i = 0; i < n; i++) {
sum[i + 1] += sum[i];
if (s[i] == '0' )
sum[i + 1]++;
if (s[i] == '1' )
tot[i + 1]++;
tot[i + 1] += tot[i];
}
int cnt = 0;
for ( int i = k; i <= n; i++) {
if (tot[i - k] == 0 and tot[n] - tot[i] == 0
and sum[i] - sum[i - k] == 0)
cnt++;
}
if (cnt == 1)
return "Yes" ;
return "No\n" ;
}
int main()
{
string s = "00?1???10?" ;
int k = 5;
cout << isValidString(s, k);
return 0;
}
|
Java
import java.util.*;
public class Main {
static String isValidString(String s, int k)
{
int n = s.length();
int [] sum = new int [n + 1 ];
Arrays.fill(sum, 0 );
int [] tot = new int [n + 1 ];
Arrays.fill(tot, 0 );
for ( int i = 0 ; i < n; i++) {
sum[i + 1 ] += sum[i];
if (s.charAt(i) == '0' )
sum[i + 1 ]++;
if (s.charAt(i) == '1' )
tot[i + 1 ]++;
tot[i + 1 ] += tot[i];
}
int cnt = 0 ;
for ( int i = k; i <= n; i++) {
if ((tot[i - k] == 0 ) && (tot[n] - tot[i] == 0 )
&& (sum[i] - sum[i - k] == 0 ))
cnt++;
}
if (cnt == 1 )
return "Yes" ;
return "No\n" ;
}
public static void main(String[] args)
{
String s = "00?1???10?" ;
int k = 5 ;
System.out.println(isValidString(s, k));
}
}
|
Python3
def isValidString(s, k):
n = len (s)
sum_arr = [ 0 ] * (n + 1 )
tot = [ 0 ] * (n + 1 )
for i in range (n):
sum_arr[i + 1 ] + = sum_arr[i]
if s[i] = = '0' :
sum_arr[i + 1 ] + = 1
if s[i] = = '1' :
tot[i + 1 ] + = 1
tot[i + 1 ] + = tot[i]
cnt = 0
for i in range (k, n + 1 ):
if tot[i - k] = = 0 and tot[n] - tot[i] = = 0 and sum_arr[i] - sum_arr[i - k] = = 0 :
cnt + = 1
if cnt = = 1 :
return "Yes"
return "No\n"
if __name__ = = '__main__' :
s = "00?1???10?"
k = 5
print (isValidString(s, k))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static string IsValidString( string s, int k)
{
int n = s.Length;
List< int > sum = new List< int >(n + 1);
List< int > tot = new List< int >(n + 1);
for ( int i = 0; i <= n; i++) {
sum.Add(0);
tot.Add(0);
}
for ( int i = 0; i < n; i++) {
sum[i + 1] += sum[i];
if (s[i] == '0' )
sum[i + 1]++;
if (s[i] == '1' )
tot[i + 1]++;
tot[i + 1] += tot[i];
}
int cnt = 0;
for ( int i = k; i <= n; i++) {
if (tot[i - k] == 0 && tot[n] - tot[i] == 0
&& sum[i] - sum[i - k] == 0) {
cnt++;
}
}
if (cnt == 1)
return "Yes" ;
return "No\n" ;
}
public static void Main()
{
string s = "00?1???10?" ;
int k = 5;
Console.WriteLine(IsValidString(s, k));
}
}
|
Javascript
function isValidString(s, k) {
let n = s.length;
let sum = Array(n + 1).fill(0);
let tot = Array(n + 1).fill(0);
for (let i = 0; i < n; i++) {
sum[i + 1] += sum[i];
if (s[i] == '0' )
sum[i + 1]++;
if (s[i] == '1' )
tot[i + 1]++;
tot[i + 1] += tot[i];
}
let cnt = 0;
for (let i = k; i <= n; i++) {
if (tot[i - k] == 0 && tot[n] - tot[i] == 0
&& sum[i] - sum[i - k] == 0)
cnt++;
}
if (cnt == 1)
return "Yes" ;
return "No\n" ;
}
let s = "00?1???10?" ;
let k = 5;
console.log(isValidString(s, k));
|
Time Complexity: O(|s|)
Auxiliary Space: O(|s|)
Share your thoughts in the comments
Please Login to comment...