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:
- 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
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Maximum number of groups of size 3 containing two type of items
- 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"
- Count of operations to make a binary string"ab" free
- Minimum swaps required to make a binary string alternating
- Number of ways to split a binary number such that every part is divisible by 2
- Check if a binary string contains all permutations of length k
- Ways to form a group from three groups with given constraints
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Length of longest consecutive ones by at most one swap in a Binary String
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.