The Wayback Machine - https://web.archive.org/web/20210821035303/https://www.geeksforgeeks.org/check-if-substring-10-occurs-in-the-given-binary-string-in-all-possible-replacements-of-with-1-or-0/
Skip to content
Related Articles

Related Articles

Improve Article

Check if substring “10” occurs in the given binary string in all possible replacements of ‘?’ with 1 or 0

  • Last Updated : 20 Aug, 2021

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 ‘?’:

  1. Replacing the ‘?’ with 0 modifies the string to “100”. In the modifies string, the substring “10” occurrs.
  2. Replacing the ‘?’ with 1 modifies the string to “110”. In the modifies string, the substring “10” occurrs.

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: 

Python3




# Python program for the above approach
  
# Function to check it possible to get
# "10" as substring after all possible
# replacements
def check(S, n):
  
    # Initialize empty string ans
    ans = ""
    c = 0
  
    # Run loop n times
    for _ in range(n):
  
        # If char is "?", then increment
        # c by 1
        if S[_] == "?":
            c += 1
        else:
  
            # Continuous '?' characters
            if c:
                ans += "?"
  
            # Their is no consective "?"
            c = 0
            ans += S[_]
              
    # "?" still left
    if c:
        ans += "?"
  
    # Check if "10" or "1?0" exists
    # in the string ans or not
    if "10" in ans or "1?0" in ans:
        return "Yes"
    else:
        return "No"
  
  
# Driver Code
if __name__ == '__main__':
  
    S = "1?0"
    ans = check(S, len(S))
    print(ans)
Output:
Yes

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :