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++
// C++ Program to implement // the above approach #include <iostream> using namespace std; // Function to check it possible to get // "10" as substring after all possible // replacements string check(string S, int n) { // Initialize empty string ans string ans = "" ; int c = 0; // Run loop n times for ( int i = 0; i < n; i++) { // If char is "?", then increment // c by 1 if (S[i] == '?' ) { c++; } else { // Continuous '?' characters if (c) { ans += "?" ; } c = 0; ans += S[i]; } } // Their is no consecutive "?" if (c) { ans += "?" ; } // Check if "10" or "1?0" exists // in the string ans or not if (ans.find( "10" ) != -1 || ans.find( "1?0" ) != -1) { return "Yes" ; } else { return "No" ; } } // Driver code int main() { string S = "1?0" ; int n = S.size(); string ans = check(S, n); cout << ans; return 0; } // This code is contributed by parthmanchanda81 |
Java
// Java program for the above approach import java.io.*; class GFG { // Returns true if s1 is substring of s2 static int isSubstring(String s1, String s2) { int M = s1.length(); int N = s2.length(); /* A loop to slide pat[] one by one */ for ( int i = 0 ; i <= N - M; i++) { int j; /* For current index i, check for pattern match */ for (j = 0 ; j < M; j++) if (s2.charAt(i + j) != s1.charAt(j)) break ; if (j == M) return i; } return - 1 ; } // Function to check it possible to get // "10" as substring after all possible // replacements static String check(String S, int n) { // Initialize empty string ans String ans = "" ; int c = 0 ; // Run loop n times for ( int i = 0 ; i < n; i++) { // If char is "?", then increment // c by 1 if (S.charAt(i) == '?' ) { c++; } else { // Continuous '?' characters if (c != 0 ) { ans += "?" ; } c = 0 ; ans += S.charAt(i); } } // Their is no consecutive "?" if (c != 0 ) { ans += "?" ; } // Check if "10" or "1?0" exists // in the string ans or not if (isSubstring( "10" , S) != - 1 || isSubstring( "1?0" , S) != - 1 ) { return "Yes" ; } else { return "No" ; } } // Driver Code public static void main (String[] args) { String S = "1?0" ; int n = S.length(); String ans = check(S, n); System.out.println(ans); } } // This code is contributed by avijitmondal1998. |
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 consecutive "?" 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) |
C#
// C# program for the approach using System; using System.Collections.Generic; class GFG { // Returns true if s1 is substring of s2 static int isSubstring( string s1, string s2) { int M = s1.Length; int N = s2.Length; /* A loop to slide pat[] one by one */ for ( int i = 0; i <= N - M; i++) { int j; /* For current index i, check for pattern match */ for (j = 0; j < M; j++) if (s2[i + j] != s1[j]) break ; if (j == M) return i; } return -1; } // Function to check it possible to get // "10" as substring after all possible // replacements static string check( string S, int n) { // Initialize empty string ans string ans = "" ; int c = 0; // Run loop n times for ( int i = 0; i < n; i++) { // If char is "?", then increment // c by 1 if (S[i] == '?' ) { c++; } else { // Continuous '?' characters if (c != 0) { ans += "?" ; } c = 0; ans += S[i]; } } // Their is no consecutive "?" if (c != 0) { ans += "?" ; } // Check if "10" or "1?0" exists // in the string ans or not if (isSubstring( "10" , S) != -1 || isSubstring( "1?0" , S) != -1) { return "Yes" ; } else { return "No" ; } } // Driver Code public static void Main() { string S = "1?0" ; int n = S.Length; string ans = check(S, n); Console.Write(ans); } } // This code is contributed by sanjoy_62. |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to check it possible to get // "10" as substring after all possible // replacements function check(S, n) { // Initialize empty string ans ans = "" c = 0 // Run loop n times for (let i = 0; i < n; i++) { // If char is "?", then increment // c by 1 if (S[i] == "?" ) c += 1 else // Continuous '?' characters if (c != 0) ans += "?" // Their is no consecutive "?" c = 0 ans += S[i] // "?" still left if (c != 0) ans += "?" } // Check if "10" or "1?0" exists // in the string ans or not if (ans.includes( '10' ) || ans.includes( '1?0' )) return "Yes" else return "No" } // Driver Code S = "1?0" ans = check(S, S.length) document.write(ans) // This code is contributed by Potta Lokesh </script> |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!