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
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.
- Count binary strings with k times appearing adjacent two set bits
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- 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 twice zeros in first half
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Generate Binary Strings of length N using Branch and Bound
- Minimum length substring with exactly K distinct characters
- Number of binary strings such that there is no substring of length ≥ 3
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- 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 numbers appearing in the given ranges at-least K times
- Longest subsequence with at least one character appearing in every string
- Find the elements appearing even number of times in an Array
- Generate all binary strings from given pattern
- Generate all binary strings without consecutive 1's
- Generate all the binary strings of N bits
- Sum of first N natural numbers with all powers of 2 added twice
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.