Number of ways to make binary string of length N such that 0s always occur together in groups of size K

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.
Examples:Â
Â
Input: N = 3, K = 2Â
Output : 3Â
No of binary strings:Â
111Â
100Â
001
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.
Thus,Â
Â
dp[i] = dp[i - 1] + dp[i - k]
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
const int mod = 1000000007;Â
// Function to return no of ways to build a binary // string of length N such that 0s always occur // in groups of size Kint noOfBinaryStrings(int N, int k){Â Â Â Â int dp[100002];Â Â Â Â for (int i = 1; i <= k - 1; i++) {Â Â Â Â Â Â Â Â dp[i] = 1;Â Â Â Â }Â
    dp[k] = 2;Â
    for (int i = k + 1; i <= N; i++) {        dp[i] = (dp[i - 1] + dp[i - k]) % mod;    }Â
    return dp[N];}Â
// Driver Codeint main(){Â Â Â Â int N = 4;Â Â Â Â int K = 2;Â Â Â Â cout << noOfBinaryStrings(N, K);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG {Â Â Â Â Â static int mod = 1000000007;Â
// Function to return no of ways to build a binary // string of length N such that 0s always occur // in groups of size Kstatic int noOfBinaryStrings(int N, int k){Â Â Â Â int dp[] = new int[100002];Â Â Â Â for (int i = 1; i <= k - 1; i++) Â Â Â Â {Â Â Â Â Â Â Â Â dp[i] = 1;Â Â Â Â }Â
    dp[k] = 2;Â
    for (int i = k + 1; i <= N; i++)     {        dp[i] = (dp[i - 1] + dp[i - k]) % mod;    }Â
    return dp[N];}Â
// Driver Codepublic static void main(String[] args) {Â Â Â Â int N = 4;Â Â Â Â int K = 2;Â Â Â Â System.out.println(noOfBinaryStrings(N, K));}}Â
// This code contributed by Rajput-Ji |
Python3
# Python3 implementation of the# above approach mod = 1000000007; Â
# Function to return no of ways to # build a binary string of length N# such that 0s always occur in # groups of size K def noOfBinaryStrings(N, k) :Â Â Â Â dp = [0] * 100002; Â Â Â Â for i in range(1, K) : Â Â Â Â Â Â Â Â dp[i] = 1; Â Â Â Â Â Â Â Â Â dp[k] = 2; Â
    for i in range(k + 1, N + 1) :        dp[i] = (dp[i - 1] + dp[i - k]) % mod; Â
    return dp[N]; Â
# Driver Code if __name__ == "__main__" : Â
    N = 4;     K = 2;          print(noOfBinaryStrings(N, K)); Â
# This code is contributed by Ryuga |
C#
// C# implementation of the approachusing System;Â
class GFG {Â Â Â Â Â static int mod = 1000000007;Â
// Function to return no of ways to build // a binary string of length N such that // 0s always occur in groups of size Kstatic int noOfBinaryStrings(int N, int k){Â Â Â Â int []dp = new int[100002];Â Â Â Â for (int i = 1; i <= k - 1; i++) Â Â Â Â {Â Â Â Â Â Â Â Â dp[i] = 1;Â Â Â Â }Â
    dp[k] = 2;Â
    for (int i = k + 1; i <= N; i++)     {        dp[i] = (dp[i - 1] + dp[i - k]) % mod;    }Â
    return dp[N];}Â
// Driver Codepublic static void Main() {Â Â Â Â int N = 4;Â Â Â Â int K = 2;Â Â Â Â Console.WriteLine(noOfBinaryStrings(N, K));}}Â
/* This code contributed by PrinciRaj1992 */ |
Javascript
<script>// Javascript implementation of the approachÂ
let mod = 1000000007;Â
// Function to return no of ways to build a binary // string of length N such that 0s always occur // in groups of size Kfunction noOfBinaryStrings(N,k){Â Â Â Â let dp = new Array(100002);Â Â Â Â for (let i = 1; i <= k - 1; i++) Â Â Â Â {Â Â Â Â Â Â Â Â dp[i] = 1;Â Â Â Â }Â Â Â Â Â Â Â dp[k] = 2;Â Â Â Â Â Â Â for (let i = k + 1; i <= N; i++) Â Â Â Â {Â Â Â Â Â Â Â Â dp[i] = (dp[i - 1] + dp[i - k]) % mod;Â Â Â Â }Â Â Â Â Â Â Â return dp[N];}Â
// Driver Codelet N = 4;let K = 2;document.write(noOfBinaryStrings(N, K));Â
// This code is contributed by rag2127.</script> |
PHP
<?php// PHP implementation of the above approach$mod = 1000000007;Â
// Function to return no of ways to // build a binary string of length N // such that 0s always occur in groups// of size Kfunction noOfBinaryStrings($N, $k){Â Â Â Â global $mod;Â Â Â Â $dp = array(0, 100002, NULL);Â Â Â Â for ($i = 1; $i <= $k - 1; $i++)Â Â Â Â {Â Â Â Â Â Â Â Â $dp[$i] = 1;Â Â Â Â }Â
    $dp[$k] = 2;Â
    for ($i = $k + 1; $i <= $N; $i++)     {        $dp[$i] = ($dp[$i - 1] +                    $dp[$i - $k]) % $mod;    }Â
    return $dp[$N];}Â
// Driver Code$N = 4;$K = 2;echo noOfBinaryStrings($N, $K);Â
// This code is contributed by ita_c ?> |
5
Another Approach: Recursion + memoization
In this approach we solve the problem with the help of recursive call and use a dp array to check that we previously computed the same subproblem.
Implementation Steps:
- Create a array of dp and initialize it with -1 to check that we previous computed the same subproblem.
- Initialize base cases.
- If computed then return dp[n].
- Create a variable ans to store the final result.
- Now recursively call the function first n-1 and second with n-k.
- At last return answer store in ans.Â
Implementation:
C++
// C++ program for above approachÂ
#include <bits/stdc++.h>using namespace std;// to handle large valuesconst int mod = 1000000007;Â
// to store subproblemsint dp[100002];Â
// Function to return no of ways to build a binary // string of length N such that 0s always occur // in groups of size Kint countBinaryStrings(int n, int k) {         // Base Case    if (n <= 0) {        return 1;    }         // previous computed    if (dp[n] != -1) {        return dp[n];    }         // recursive calls    int ans = (countBinaryStrings(n-1, k) + ((n >= k) ? countBinaryStrings(n-k, k) : 0)) % mod;         // update DP    dp[n] = ans;         // return final answer    return ans;}Â
// Driver codeint main() {    int n = 4, k = 2;         // fill dp with -1    memset(dp, -1, sizeof(dp));         // function call    cout << countBinaryStrings(n, k) << endl;    return 0;} |
Java
// Java program for above approachÂ
import java.util.Arrays;Â
public class GFG {    // to handle large values    static final int mod = 1000000007;Â
    // to store subproblems    static int[] dp;Â
    // Function to return the number of ways to build a binary     // string of length N such that 0s always occur     // in groups of size K    static int countBinaryStrings(int n, int k) {        // Base Case        if (n <= 0) {            return 1;        }Â
        // Previous computed        if (dp[n] != -1) {            return dp[n];        }Â
        // Recursive calls        int ans = (countBinaryStrings(n - 1, k) + ((n >= k) ? countBinaryStrings(n - k, k) : 0)) % mod;Â
        // Update DP        dp[n] = ans;Â
        // Return the final answer        return ans;    }Â
    // Driver code    public static void main(String[] args) {        int n = 4, k = 2;Â
        // Fill dp with -1        dp = new int[n + 1];        Arrays.fill(dp, -1);Â
        // Function call        System.out.println(countBinaryStrings(n, k));    }} |
Python3
# to handle large valuesmod = 1000000007Â
# to store subproblemsdp = [-1] * 100002Â
# Function to return the number of ways to build a binary # string of length N such that 0s always occur in groups of size KÂ
Â
def countBinaryStrings(n, k):    # Base Case    if n == 0:        return 1Â
    # Check if the subproblem is already computed    if dp[n] != -1:        return dp[n]Â
    # Recursive calls    ans = (countBinaryStrings(n - 1, k) +           (countBinaryStrings(n - k, k) if n >= k else 0)) % modÂ
    # Update DP    dp[n] = ansÂ
    # Return the final answer    return ansÂ
Â
# Driver coden = 4k = 2Â
# Fill dp with -1dp = [-1] * (n + 1)Â
# Function callprint(countBinaryStrings(n, k))Â
# This code is contributed by rambabuguphka |
C#
using System;Â
public class GFG {    // to handle large values    const int mod = 1000000007;Â
    // to store subproblems    static int[] dp;Â
    // Function to return the number of ways to build a    // binary string of length N such that 0s always occur    // in groups of size K    static int CountBinaryStrings(int n, int k)    {        // Base Case        if (n <= 0) {            return 1;        }Â
        // previous computed        if (dp[n] != -1) {            return dp[n];        }Â
        // recursive calls        int ans            = (CountBinaryStrings(n - 1, k)               + ((n >= k) ? CountBinaryStrings(n - k, k)                           : 0))              % mod;Â
        // update DP        dp[n] = ans;Â
        // return the final answer        return ans;    }Â
    // Driver code    static void Main()    {        int n = 4, k = 2;Â
        // initialize dp with -1        dp = new int[100002];        for (int i = 0; i < dp.Length; i++) {            dp[i] = -1;        }Â
        // function call        Console.WriteLine(CountBinaryStrings(n, k));    }} |
Javascript
// to handle large valuesconst mod = 1000000007;Â
// to store subproblemslet dp = new Array(100002);Â
// Function to return no of ways to build a binary // string of length N such that 0s always occur // in groups of size Kfunction countBinaryStrings(n, k) {    // Base Case    if (n <= 0) {        return 1;    }Â
    // previous computed    if (dp[n] != -1) {        return dp[n];    }Â
    // recursive calls    let ans = (countBinaryStrings(n - 1, k) + ((n >= k) ?                countBinaryStrings(n - k, k) : 0)) % mod;Â
    // update DP    dp[n] = ans;Â
    // return final answer    return ans;}Â
// Driver codelet n = 4,    k = 2;Â
// fill dp with -1dp.fill(-1);Â
// function callconsole.log(countBinaryStrings(n, k)); |
Output:
5
Time complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



