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.
Input: str = “0110″, N = 3
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
Since substrings “0″, “1″, “10″, and “11″ can be formed from given string, but not “100”. Hence the answer is False
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of substrings of one string present in other
- Binary Representations in Digital Logic
- Check if the given string of words can be formed from words present in the dictionary
- Python | Check if a Substring is Present in a Given String
- Check if given words are present in a string
- Graph representations using set and hash
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Given a binary string, count number of substrings that start and end with 1.
- Count of substrings of a given Binary string with all characters same
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Reverse the substrings of the given String according to the given Array of indices
- Check whether the Average Character of the String is present or not
- Remove characters from the first string which are present in the second string
- Find the character in first string that is present at minimum index in second string
- Most frequent word in first String which is not present in second String
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Check if all array elements are present in a given stack or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.