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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to convert decimal to binary
// representation
string decimalToBinary(int N)
{
string ans = "";
// Iterate over all bits of N
while (N > 0) {
// If bit is 1
if (N & 1) {
ans = '1' + ans;
}
else {
ans = '0' + ans;
}
N /= 2;
}
// Return binary representation
return ans;
}
// Function to check if binary conversion
// of numbers from N to 1 exists in the
// string as a substring or not
string checkBinaryString(string& str, int N)
{
// To store the count of number
// exists as a substring
int map[N + 10], cnt = 0;
memset(map, 0, sizeof(map));
// Traverse from N to 1
for (int i = N; i > 0; i--) {
// If current number is not
// present in map
if (!map[i]) {
// Store current number
int t = i;
// Find binary of t
string s = decimalToBinary(t);
// If the string s is a
// substring of str
if (str.find(s) != str.npos) {
while (t && !map[t]) {
// Mark t as true
map[t] = 1;
// Increment the count
cnt++;
// Update for t/2
t >>= 1;
}
}
}
}
// Special judgement '0'
for (int i = 0; i < str.length(); i++) {
if (str[i] == '0') {
cnt++;
break;
}
}
// If the count is N+1, return "yes"
if (cnt == N + 1)
return "True";
else
return "False";
}
// Driver Code
int main()
{
// Given String
string str = "0110";
// Given Number
int N = 3;
// Function Call
cout << checkBinaryString(str, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to convert decimal to binary
// representation
static String decimalToBinary(int N)
{
String ans = "";
// Iterate over all bits of N
while (N > 0)
{
// If bit is 1
if (N % 2 == 1)
{
ans = '1' + ans;
}
else
{
ans = '0' + ans;
}
N /= 2;
}
// Return binary representation
return ans;
}
// Function to check if binary conversion
// of numbers from N to 1 exists in the
// String as a subString or not
static String checkBinaryString(String str, int N)
{
// To store the count of number
// exists as a subString
int []map = new int[N + 10];
int cnt = 0;
// Traverse from N to 1
for(int i = N; i > 0; i--)
{
// If current number is not
// present in map
if (map[i] == 0)
{
// Store current number
int t = i;
// Find binary of t
String s = decimalToBinary(t);
// If the String s is a
// subString of str
if (str.contains(s))
{
while (t > 0 && map[t] == 0)
{
// Mark t as true
map[t] = 1;
// Increment the count
cnt++;
// Update for t/2
t >>= 1;
}
}
}
}
// Special judgement '0'
for(int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == '0')
{
cnt++;
break;
}
}
// If the count is N+1, return "yes"
if (cnt == N + 1)
return "True";
else
return "False";
}
// Driver Code
public static void main(String[] args)
{
// Given String
String str = "0110";
// Given number
int N = 3;
// Function call
System.out.print(checkBinaryString(str, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of
# the above approach
# Function to convert decimal to
# binary representation
def decimalToBinary(N):
ans = ""
# Iterate over all bits of N
while(N > 0):
# If bit is 1
if(N & 1):
ans = '1' + ans
else:
ans = '0' + ans
N //= 2
# Return binary representation
return ans
# Function to check if binary conversion
# of numbers from N to 1 exists in the
# string as a substring or not
def checkBinaryString(str, N):
# To store the count of number
# exists as a substring
map = [0] * (N + 10)
cnt = 0
# Traverse from N to 1
for i in range(N, -1, -1):
# If current number is not
# present in map
if(not map[i]):
# Store current number
t = i
# Find binary of t
s = decimalToBinary(t)
# If the string s is a
# substring of str
if(s in str):
while(t and not map[t]):
# Mark t as true
map[t] = 1
# Increment the count
cnt += 1
# Update for t/2
t >>= 1
# Special judgement '0'
for i in range(len(str)):
if(str[i] == '0'):
cnt += 1
break
# If the count is N+1, return "yes"
if(cnt == N + 1):
return "True"
else:
return "False"
# Driver Code
if __name__ == '__main__':
# Given String
str = "0110"
# Given Number
N = 3
# Function Call
print(checkBinaryString(str, N))
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function to convert decimal to binary
// representation
static String decimalToBinary(int N)
{
String ans = "";
// Iterate over all bits of N
while (N > 0)
{
// If bit is 1
if (N % 2 == 1)
{
ans = '1' + ans;
}
else
{
ans = '0' + ans;
}
N /= 2;
}
// Return binary representation
return ans;
}
// Function to check if binary conversion
// of numbers from N to 1 exists in the
// String as a subString or not
static String checkBinaryString(String str, int N)
{
// To store the count of number
// exists as a subString
int []map = new int[N + 10];
int cnt = 0;
// Traverse from N to 1
for(int i = N; i > 0; i--)
{
// If current number is not
// present in map
if (map[i] == 0)
{
// Store current number
int t = i;
// Find binary of t
String s = decimalToBinary(t);
// If the String s is a
// subString of str
if (str.Contains(s))
{
while (t > 0 && map[t] == 0)
{
// Mark t as true
map[t] = 1;
// Increment the count
cnt++;
// Update for t/2
t >>= 1;
}
}
}
}
// Special judgement '0'
for(int i = 0; i < str.Length; i++)
{
if (str[i] == '0')
{
cnt++;
break;
}
}
// If the count is N+1, return "yes"
if (cnt == N + 1)
return "True";
else
return "False";
}
// Driver Code
public static void Main(String[] args)
{
// Given String
String str = "0110";
// Given number
int N = 3;
// Function call
Console.Write(checkBinaryString(str, N));
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// Javascript program for the above approach
// Function to convert decimal to binary
// representation
function decimalToBinary(N)
{
var ans = "";
// Iterate over all bits of N
while (N > 0) {
// If bit is 1
if (N % 2 == 1){
ans = '1' + ans;
}
else {
ans = '0' + ans;
}
N = parseInt(N/2);
}
// Return binary representation
return ans;
}
// Function to check if binary conversion
// of numbers from N to 1 exists in the
// string as a substring or not
function checkBinaryString(str, N)
{
// To store the count of number
// exists as a substring
var map = Array(N+10).fill(0), cnt = 0;
// Traverse from N to 1
for (var i = N; i > 0; i--) {
// If current number is not
// present in map
if (!map[i]) {
// Store current number
var t = i;
// Find binary of t
var s = decimalToBinary(t);
// If the string s is a
// substring of str
if (str.includes(s)) {
while (t>0 && map[t] == 0) {
// Mark t as true
map[t] = 1;
// Increment the count
cnt++;
// Update for t/2
t >>= 1;
}
}
}
}
// Special judgement '0'
for (var i = 0; i < str.length; i++) {
if (str[i] == '0') {
cnt++;
break;
}
}
// If the count is N+1, return "yes"
if (cnt == N + 1)
return "True";
else
return "False";
}
// Driver Code
// Given String
var str = "0110";
// Given Number
var N = 3;
// Function Call
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
Similar Reads
Check if substring "10" occurs in the given binary string in all possible replacements of '?' with 1 or 0 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: YesExplanation:Following are all the possible replacements of '?': Replacing the '?' wi
7 min read
Check if given number contains only â01â and â10â as substring in its binary representation Given a number N, the task is to check if the binary representation of the number N has only "01" and "10" as a substring or not. If found to be true, then print "Yes". Otherwise, print "No".Examples: Input: N = 5 Output: Yes Explanation: (5)10 is (101)2 which contains only "01" and "10" as substrin
6 min read
Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin
9 min read
Find all substrings with even 1s whose reverse is also present in given String Given a binary string str. The task is to find the size of the set(contains unique substrings) of substrings such that if there is a substring(suppose A) of length n with an even number of 1's and also there is another substring (suppose B) of the same length n and even number of 1's and it is rever
7 min read
Count of substrings of a given Binary string with all characters same Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same. Examples: Input: str = â011âOutput: 4Explanation: Three sub-strings are "1", "1", "11" which have only 1 in them, and one substring is there
10 min read
Count of substrings that start and end with 1 in given Binary String Given a binary string s, the task is to count all substrings that start and end with the character '1'. A valid substring must have both its first and last characters as '1', and can include one or more number of characters in between.Examples:Input: s = "00100101"Output: 3Explanation: Valid substri
7 min read