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++
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int 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];
}
int main()
{
int N = 4;
int K = 2;
cout << noOfBinaryStrings(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int mod = 1000000007 ;
static 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];
}
public static void main(String[] args)
{
int N = 4 ;
int K = 2 ;
System.out.println(noOfBinaryStrings(N, K));
}
}
|
Python3
mod = 1000000007 ;
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];
if __name__ = = "__main__" :
N = 4 ;
K = 2 ;
print (noOfBinaryStrings(N, K));
|
C#
using System;
class GFG
{
static int mod = 1000000007;
static 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];
}
public static void Main()
{
int N = 4;
int K = 2;
Console.WriteLine(noOfBinaryStrings(N, K));
}
}
|
Javascript
<script>
let mod = 1000000007;
function 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];
}
let N = 4;
let K = 2;
document.write(noOfBinaryStrings(N, K));
</script>
|
PHP
<?php
$mod = 1000000007;
function 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 ];
}
$N = 4;
$K = 2;
echo noOfBinaryStrings( $N , $K );
?>
|
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++
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int dp[100002];
int countBinaryStrings( int n, int k) {
if (n <= 0) {
return 1;
}
if (dp[n] != -1) {
return dp[n];
}
int ans = (countBinaryStrings(n-1, k) + ((n >= k) ? countBinaryStrings(n-k, k) : 0)) % mod;
dp[n] = ans;
return ans;
}
int main() {
int n = 4, k = 2;
memset (dp, -1, sizeof (dp));
cout << countBinaryStrings(n, k) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static final int mod = 1000000007 ;
static int [] dp;
static int countBinaryStrings( int n, int k) {
if (n <= 0 ) {
return 1 ;
}
if (dp[n] != - 1 ) {
return dp[n];
}
int ans = (countBinaryStrings(n - 1 , k) + ((n >= k) ? countBinaryStrings(n - k, k) : 0 )) % mod;
dp[n] = ans;
return ans;
}
public static void main(String[] args) {
int n = 4 , k = 2 ;
dp = new int [n + 1 ];
Arrays.fill(dp, - 1 );
System.out.println(countBinaryStrings(n, k));
}
}
|
Python3
mod = 1000000007
dp = [ - 1 ] * 100002
def countBinaryStrings(n, k):
if n = = 0 :
return 1
if dp[n] ! = - 1 :
return dp[n]
ans = (countBinaryStrings(n - 1 , k) +
(countBinaryStrings(n - k, k) if n > = k else 0 )) % mod
dp[n] = ans
return ans
n = 4
k = 2
dp = [ - 1 ] * (n + 1 )
print (countBinaryStrings(n, k))
|
C#
using System;
public class GFG {
const int mod = 1000000007;
static int [] dp;
static int CountBinaryStrings( int n, int k)
{
if (n <= 0) {
return 1;
}
if (dp[n] != -1) {
return dp[n];
}
int ans
= (CountBinaryStrings(n - 1, k)
+ ((n >= k) ? CountBinaryStrings(n - k, k)
: 0))
% mod;
dp[n] = ans;
return ans;
}
static void Main()
{
int n = 4, k = 2;
dp = new int [100002];
for ( int i = 0; i < dp.Length; i++) {
dp[i] = -1;
}
Console.WriteLine(CountBinaryStrings(n, k));
}
}
|
Javascript
const mod = 1000000007;
let dp = new Array(100002);
function countBinaryStrings(n, k) {
if (n <= 0) {
return 1;
}
if (dp[n] != -1) {
return dp[n];
}
let ans = (countBinaryStrings(n - 1, k) + ((n >= k) ?
countBinaryStrings(n - k, k) : 0)) % mod;
dp[n] = ans;
return ans;
}
let n = 4,
k = 2;
dp.fill(-1);
console.log(countBinaryStrings(n, k));
|
Output:
5
Time complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
17 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...