Given two integers N and K, the task is to count the number of ways to make a binary string of length N such that 0s always occur together in a group of size K.
Input: N = 3, K = 2
Output : 3
No of binary strings:
Input : N = 4, K = 2
Output : 5
This problem can easily be solved using dynamic programming. Let dp[i] be the number of binary strings of length i satisfying the condition.
From the condition it can be deduced that:
- dp[i] = 1 for 1 <= i < k.
- Also dp[k] = 2 since a binary string of length K will either be a string of only zeros or only ones.
- Now if we consider for i > k. If we decide the ith character to be ‘1’, then dp[i] = dp[i-1] since the number of binary strings would not change. However if we decide the ith character to be ‘0’, then we require that previous k-1 characters should also be ‘0’ and hence dp[i] = dp[i-k]. Therefore dp[i] will be the sum of these 2 values.
dp[i] = dp[i - 1] + dp[i - k]
Below is the implementation of the above approach:
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.
- Binary String of given length that without a palindrome of size 3
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Number of flips to make binary string alternate | Set 1
- Minimum number of swaps to make two binary string equal
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Count the number of ways to divide N in k groups incrementally
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Maximum number of set bits count in a K-size substring of a Binary String
- Maximum number of groups of size 3 containing two type of items
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Number of ways to make exactly C components in a 2*N matrix
- Ways to remove one element from a binary string so that XOR becomes zero
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Number of ways to split a binary number such that every part is divisible by 2
- Count of operations to make a binary string"ab" free
- Minimum swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Ways to form a group from three groups with given constraints
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.