Check if binary representations of 0 to N are present as substrings in given binary string | Function to convert decimal to binary representation ; Iterate over all bits of N ; If bit is 1 ; Return binary representation ; Function to check if binary conversion of numbers from N to 1 exists in the string as a substring or not ; To store the count of number exists as a substring ; Traverse from N to 1 ; If current number is not present in map ; Store current number ; Find binary of t ; If the string s is a substring of str ; Mark t as true ; Increment the count ; Update for t / 2 ; Special judgment '0 ; If the count is N + 1 , return " yes " ; Driver Code ; Given String ; Given Number ; Function Call
def decimalToBinary ( N ) : NEW_LINE INDENT ans = " " NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT ans = '1' + ans NEW_LINE DEDENT else : NEW_LINE INDENT ans = '0' + ans NEW_LINE DEDENT N //= 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT def checkBinaryString ( str , N ) : NEW_LINE INDENT map = [ 0 ] * ( N + 10 ) NEW_LINE cnt = 0 NEW_LINE for i in range ( N , - 1 , - 1 ) : NEW_LINE INDENT if ( not map [ i ] ) : NEW_LINE INDENT t = i NEW_LINE s = decimalToBinary ( t ) NEW_LINE if ( s in str ) : NEW_LINE INDENT while ( t and not map [ t ] ) : NEW_LINE INDENT map [ t ] = 1 NEW_LINE cnt += 1 NEW_LINE t >>= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT ' NEW_LINE INDENT for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT cnt += 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( cnt == N + 1 ) : NEW_LINE INDENT return " True " NEW_LINE DEDENT else : NEW_LINE INDENT return " False " NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "0110" NEW_LINE N = 3 NEW_LINE print ( checkBinaryString ( str , N ) ) NEW_LINE DEDENT