Given an integer N, the task is to generate all possible binary strings of length N which contain “01” as the sub-string exactly twice.
Input: N = 4
“0101” is the only binary string of length 4
that contains “01” exactly twice as the sub-string.
Input: N = 5
Approach: This problem can solved using backtracking. To generate a binary string, we implement a function that generate each bit at a time, update the state of the binary string (current length, number of occurrences of the pattern). Then call the function recursively, and according to the current state of the binary string, the function will decide how to generate the next bit or print out the binary string (if the problem’s requirement is met).
For this problem, backtracking strategy looks like we generate a binary tree with each node can have either value 0 or 1.
For example, with N = 4, the tree will look like:
Below is the implementation of the above approach:
00101 01001 01010 01011 01101 10101
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Generate Binary Strings of length N using Branch and Bound
- Number of binary strings such that there is no substring of length ≥ 3
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count binary strings with k times appearing adjacent two set bits
- Generate all binary strings from given pattern
- Generate all the binary strings of N bits
- Generate all binary strings without consecutive 1's
- Count number of binary strings of length N having only 0's and 1's
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Number of Binary Strings of length N with K adjacent Set Bits
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Count of same length Strings that exists lexicographically in between two given Strings
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.