Check if substring “10” occurs in the given binary string in all possible replacements of ‘?’ with 1 or 0
Last Updated :
26 Nov, 2022
Given a string S consisting of only ‘0’, ‘1’ and ‘?’, the task is to check if there exists a substring “10” in every possible replacement of the character ‘?’ with either 1 or 0.
Examples:
Input: S = “1?0”
Output: Yes
Explanation:
Following are all the possible replacements of ‘?’:
- Replacing the ‘?’ with 0 modifies the string to “100”. In the modifies string, the substring “10” occurs.
- Replacing the ‘?’ with 1 modifies the string to “110”. In the modifies string, the substring “10” occurs.
From the above all possible replacements, the substring “10” occurs in all the replacements, therefore, print Yes.
Input: S= “??”
Output: No
Approach: The given problem can be solved by using a Greedy Approach which is based on the observation that if the string S contains many consecutive ‘?’, it can be replaced with a single ‘?’ as in the worst case we can replace it with all 1s or 0s.
Therefore, the idea is to create a new string from the given string S by replacing continuous ‘?’ with single ‘?’ and then check if there exists “10” or “1?0” as a substring, then it is possible to get “10” as substring after all possible replacements, therefore, print Yes. Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
string check(string S, int n)
{
string ans = "" ;
int c = 0;
for ( int i = 0; i < n; i++) {
if (S[i] == '?' ) {
c++;
}
else {
if (c) {
ans += "?" ;
}
c = 0;
ans += S[i];
}
}
if (c) {
ans += "?" ;
}
if (ans.find( "10" ) != -1 || ans.find( "1?0" ) != -1) {
return "Yes" ;
}
else {
return "No" ;
}
}
int main()
{
string S = "1?0" ;
int n = S.size();
string ans = check(S, n);
cout << ans;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int isSubstring(String s1, String s2)
{
int M = s1.length();
int N = s2.length();
for ( int i = 0 ; i <= N - M; i++) {
int j;
for (j = 0 ; j < M; j++)
if (s2.charAt(i + j) != s1.charAt(j))
break ;
if (j == M)
return i;
}
return - 1 ;
}
static String check(String S, int n)
{
String ans = "" ;
int c = 0 ;
for ( int i = 0 ; i < n; i++) {
if (S.charAt(i) == '?' ) {
c++;
}
else {
if (c != 0 ) {
ans += "?" ;
}
c = 0 ;
ans += S.charAt(i);
}
}
if (c != 0 ) {
ans += "?" ;
}
if (isSubstring( "10" , S) != - 1 || isSubstring( "1?0" , S) != - 1 ) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void main (String[] args)
{
String S = "1?0" ;
int n = S.length();
String ans = check(S, n);
System.out.println(ans);
}
}
|
Python3
def check(S, n):
ans = ""
c = 0
for _ in range (n):
if S[_] = = "?" :
c + = 1
else :
if c:
ans + = "?"
c = 0
ans + = S[_]
if c:
ans + = "?"
if "10" in ans or "1?0" in ans:
return "Yes"
else :
return "No"
if __name__ = = '__main__' :
S = "1?0"
ans = check(S, len (S))
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int isSubstring( string s1, string s2)
{
int M = s1.Length;
int N = s2.Length;
for ( int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (s2[i + j] != s1[j])
break ;
if (j == M)
return i;
}
return -1;
}
static string check( string S, int n)
{
string ans = "" ;
int c = 0;
for ( int i = 0; i < n; i++) {
if (S[i] == '?' ) {
c++;
}
else {
if (c != 0) {
ans += "?" ;
}
c = 0;
ans += S[i];
}
}
if (c != 0) {
ans += "?" ;
}
if (isSubstring( "10" , S) != -1 || isSubstring( "1?0" , S) != -1) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void Main()
{
string S = "1?0" ;
int n = S.Length;
string ans = check(S, n);
Console.Write(ans);
}
}
|
Javascript
<script>
function check(S, n) {
ans = ""
c = 0
for (let i = 0; i < n; i++) {
if (S[i] == "?" )
c += 1
else
if (c != 0)
ans += "?"
c = 0
ans += S[i]
if (c != 0)
ans += "?"
}
if (ans.includes( '10' ) || ans.includes( '1?0' ))
return "Yes"
else
return "No"
}
S = "1?0"
ans = check(S, S.length)
document.write(ans)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Note: The same approach can be used for substrings “00”/”01″/”11″ as well with minor changes.
Share your thoughts in the comments
Please Login to comment...