Check if binary representations of 0 to N are present as substrings in given binary string
Last Updated :
18 Jun, 2022
Give binary string str and an integer N, the task is to check if the substrings of the string contain all binary representations of non-negative integers less than or equal to the given integer N.
Examples:
Input: str = “0110″, N = 3
Output: True
Explanation:
Since substrings “0″, “1″, “10″, and “11″ can be formed from given string. Hence all binary representations of 0 to 3 are present as substrings in given binary string.
Input: str = “0110”, N = 4
Output: False
Explanation:
Since substrings “0″, “1″, “10″, and “11″ can be formed from given string, but not “100”. Hence the answer is False
Approach:
The above problem can be solved using BitSet and HashMap. Follow the steps given below to solve the problem
- Initialize a map[] to mark the strings and take a bit-set variable ans to convert the number from decimal to binary.
- Take one more variable count as zero.
- run the loop from N to 1 using the variable i and check the corresponding numbers are marked in a map or not.
- if number i is not marked in a map[] then convert the current number into binary using the bit-set variable ans.
- then check if converted binary string is substring of the given string or not.
- if it is not a substring then
- run while loop unless i is not marked and binary number becomes zero
- mark the i in a map
- increment the count
- do the right shift of converted number. This is done because if any string x is converted into binary (say 111001) and this substring is already marked in map, then 11100 will already be marked automatically.
This is based on the fact that if i exists, i>>1 also exists.
- Finally check if count ? N + 1, then print True
Else print False
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string decimalToBinary( int N)
{
string ans = "" ;
while (N > 0) {
if (N & 1) {
ans = '1' + ans;
}
else {
ans = '0' + ans;
}
N /= 2;
}
return ans;
}
string checkBinaryString(string& str, int N)
{
int map[N + 10], cnt = 0;
memset (map, 0, sizeof (map));
for ( int i = N; i > 0; i--) {
if (!map[i]) {
int t = i;
string s = decimalToBinary(t);
if (str.find(s) != str.npos) {
while (t && !map[t]) {
map[t] = 1;
cnt++;
t >>= 1;
}
}
}
}
for ( int i = 0; i < str.length(); i++) {
if (str[i] == '0' ) {
cnt++;
break ;
}
}
if (cnt == N + 1)
return "True" ;
else
return "False" ;
}
int main()
{
string str = "0110" ;
int N = 3;
cout << checkBinaryString(str, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String decimalToBinary( int N)
{
String ans = "" ;
while (N > 0 )
{
if (N % 2 == 1 )
{
ans = '1' + ans;
}
else
{
ans = '0' + ans;
}
N /= 2 ;
}
return ans;
}
static String checkBinaryString(String str, int N)
{
int []map = new int [N + 10 ];
int cnt = 0 ;
for ( int i = N; i > 0 ; i--)
{
if (map[i] == 0 )
{
int t = i;
String s = decimalToBinary(t);
if (str.contains(s))
{
while (t > 0 && map[t] == 0 )
{
map[t] = 1 ;
cnt++;
t >>= 1 ;
}
}
}
}
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) == '0' )
{
cnt++;
break ;
}
}
if (cnt == N + 1 )
return "True" ;
else
return "False" ;
}
public static void main(String[] args)
{
String str = "0110" ;
int N = 3 ;
System.out.print(checkBinaryString(str, N));
}
}
|
Python3
def decimalToBinary(N):
ans = ""
while (N > 0 ):
if (N & 1 ):
ans = '1' + ans
else :
ans = '0' + ans
N / / = 2
return ans
def checkBinaryString( str , N):
map = [ 0 ] * (N + 10 )
cnt = 0
for i in range (N, - 1 , - 1 ):
if ( not map [i]):
t = i
s = decimalToBinary(t)
if (s in str ):
while (t and not map [t]):
map [t] = 1
cnt + = 1
t >> = 1
for i in range ( len ( str )):
if ( str [i] = = '0' ):
cnt + = 1
break
if (cnt = = N + 1 ):
return "True"
else :
return "False"
if __name__ = = '__main__' :
str = "0110"
N = 3
print (checkBinaryString( str , N))
|
C#
using System;
class GFG{
static String decimalToBinary( int N)
{
String ans = "" ;
while (N > 0)
{
if (N % 2 == 1)
{
ans = '1' + ans;
}
else
{
ans = '0' + ans;
}
N /= 2;
}
return ans;
}
static String checkBinaryString(String str, int N)
{
int []map = new int [N + 10];
int cnt = 0;
for ( int i = N; i > 0; i--)
{
if (map[i] == 0)
{
int t = i;
String s = decimalToBinary(t);
if (str.Contains(s))
{
while (t > 0 && map[t] == 0)
{
map[t] = 1;
cnt++;
t >>= 1;
}
}
}
}
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == '0' )
{
cnt++;
break ;
}
}
if (cnt == N + 1)
return "True" ;
else
return "False" ;
}
public static void Main(String[] args)
{
String str = "0110" ;
int N = 3;
Console.Write(checkBinaryString(str, N));
}
}
|
Javascript
<script>
function decimalToBinary(N)
{
var ans = "" ;
while (N > 0) {
if (N % 2 == 1){
ans = '1' + ans;
}
else {
ans = '0' + 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] == '0' ) {
cnt++;
break ;
}
}
if (cnt == N + 1)
return "True" ;
else
return "False" ;
}
var str = "0110" ;
var N = 3;
document.write( checkBinaryString(str, N));
</script>
|
Time Complexity: O(N logN)
Auxiliary Space: O(N), as extra space of size N is used to make an array
Share your thoughts in the comments
Please Login to comment...