Given two integers n and k, count the number of binary strings of length n with k as number of times adjacent 1’s appear.
Input : n = 5, k = 2 Output : 6 Explanation: Binary strings of length 5 in which k number of times two adjacent set bits appear. 00111 01110 11100 11011 10111 11101 Input : n = 4, k = 1 Output : 3 Explanation: Binary strings of length 3 in which k number of times two adjacent set bits appear. 0011 1100 0110
Lets try writing the recursive function for the above problem statement:
1) n = 1, only two binary strings exist with length 1, not having any adjacent 1’s
String 1 : “0”
String 2 : “1”
2) For all n > 1 and all k, two cases arise
a) Strings ending with 0 : String of length n can be created by appending 0 to all strings of length n-1 having k times two adjacent 1’s ending with both 0 and 1 (Having 0 at n’th position will not change the count of adjacent 1’s).
b) Strings ending with 1 : String of length n can be created by appending 1 to all strings of length n-1 having k times adjacent 1’s and ending with 0 and to all strings of length n-1 having k-1 adjacent 1’s and ending with 1.
Example: let s = 011 i.e. a string ending with 1 having adjacent count as 1. Adding 1 to it, s = 0111 increase the count of adjacent 1.
Let there be an array dp[i][j] where dp[i][j] denotes number of binary strings with length i having j number of two adjacent 1's and ending with 0. Similarly dp[i][j] denotes the same binary strings with length i and j adjacent 1's but ending with 1. Then: dp = 1 and dp = 1 For all other i and j, dp[i][j] = dp[i-1][j] + dp[i-1][j] dp[i][j] = dp[i-1][j] + dp[i-1][j-1] Then, output dp[n][k] + dp[n][k]
Time Complexity : O(n2)
This article is contributed by Ekta Goel. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 Binary Strings of length N with K adjacent Set Bits
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Generate all the binary strings of N bits
- 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
- Count of strings where adjacent characters are of difference one
- Count even length binary sequences with same sum of first and second half bits
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's
- Maximum number of set bits count in a K-size substring of a Binary String
- Count of Binary Strings possible as per given conditions
- Count number of binary strings without consecutive 1's
- Count number of binary strings without consecutive 1’s : Set 2
- Count of binary strings of given length consisting of at least one 1
- Count binary strings with twice zeros in first half
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- Count of distinct XORs formed by rearranging two Binary strings
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's