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
- 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
- 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 longest valid substring
- Length of the longest substring that do not contain any palindrome
- Longest Even Length Substring such that Sum of First and Second Half is same
- Maximum length substring having all same characters after k changes
- Length of the longest substring with equal 1s and 0s
- Minimum K such that every substring of length atleast K contains a character c
- Length of the longest substring with no consecutive same letters
- Length of the longest substring without repeating characters
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.