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 " ; Given String ; Given Number ; Function Call
function decimalToBinary ( N ) { var ans = " " ; while ( N > 0 ) { if ( N % 2 == 1 ) { ans = ' ' + ans ; } else { ans = ' ' + ans ; } N = parseInt ( N / 2 ) ; } return ans ; } function checkBinaryString ( str , N ) { var map = Array ( N + 10 ) . fill ( 0 ) , cnt = 0 ; for ( var i = N ; i > 0 ; i -- ) { if ( ! map [ i ] ) { var t = i ; var s = decimalToBinary ( t ) ; if ( str . includes ( s ) ) { while ( t > 0 && map [ t ] == 0 ) { map [ t ] = 1 ; cnt ++ ; t >>= 1 ; } } } } for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) { cnt ++ ; break ; } } if ( cnt == N + 1 ) return " " ; else return " " ; } var str = " " ; var N = 3 ; document . write ( checkBinaryString ( str , N ) ) ;