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
- 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 the binary strings of N bits
- Generate all binary strings without consecutive 1's
- Generate all binary strings from given pattern
- Count number of binary strings of length N having only 0's and 1's
- Number of Binary Strings of length N with K adjacent Set Bits
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Length of the longest substring that do not contain any palindrome
- Maximum length substring having all same characters after k changes
- Find max length odd parity substring
- Length of the longest valid substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.