Consecutive 1's not allowed | Practice | GeeksforGeeks

Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7.
Example 1:
Input:
N = 3
Output: 5
Explanation: 5 strings are (000,
001, 010, 100, 101).

Example 2:
Input:
N = 2
Output: 3
Explanation: 3 strings are
(00,01,10).

Your Task:
Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 105
+1
kashyapjhon5 days ago
C++ Solution Time=(0.01/1.12) EASY:
ll countStrings(int n) {    // code here    vector<ll> dp(n+1,0);    dp[0]=1;    dp[1]=2;    for(int i=2;i<=n;i++){        dp[i]=(dp[i-1]+dp[i-2])%1000000007;    }    return dp[n];}
0
manjeetdhaterwal3 weeks ago
java sol
long countStrings(int n) {

        int f = 1;
        int s = 2;
        int t = 0;
        
        if(n==0) return f;
        if(n==1) return s;
        
        for(int i=2; i<=n; i++)
        {
            t = (f+s)%1000000007;
            f = s;
            s = t;
        }
        return t;
    }
0
parikhsharan61 month ago
// JAVA solutoin Top down approach, 0.2s
 
class Solution {   long dp[][];   private final long MOD = 1000000007;   long countStrings(int n) {       dp = new long[n + 1][2];       for(long[] a : dp) {           Arrays.fill(a, -1);       }       return helper(n, 0);   }      private long helper(int n, int isPrevOne) {              if(n == 0) {           return 1;       }              if(dp[n][isPrevOne] != -1) {           return dp[n][isPrevOne];       }              if(isPrevOne == 1) {           return dp[n][isPrevOne] = helper(n - 1, 0) % MOD;       }       else {           return dp[n][isPrevOne] = (helper(n - 1, 0) % MOD + helper(n - 1, 1) % MOD) % MOD;       }          }}
0
50n_0f_b3n9a12 months ago
	PYTHON(0.1 sec)---
	
	def countStrings(self,n):
    	z = 1;o = 0;
    	for i in range(1,n+1):
    	    z,o = z + o, z;
    	return (z+o)%1000000007;
+1
kashifamir2742 months ago
perfect solution doesnt exist
 
ll countStrings(int n) {    int arr[n+1];    arr[0]=1;    arr[1]=2;    for( int i=2;i<=n; i++)    {        arr[i]=(arr[i-1]+arr[i-2])%1000000007;    }    return arr[n];}
0
rajeevsahu852 months ago
     if(n==1) return 2;    if(n==2) return 3;    ll mod=1e9+7;    ll prev_prev=2;    ll prev=3;    int ans=0;    for(int i=3;i<=n;i++){        ans=(prev+prev_prev)%mod;        prev_prev=prev;        prev=ans;    }    return ans%mod;
0
lietester00
This comment was deleted.
+1
rahuluttam9382 months ago
class Solution{public:// #define ll long longll dp[100002][2];ll mod=1000000007;ll slove(int n, int take_zero,  int take_one){   if(n==0){       return 1;   }      if(take_zero==true && dp[n][0]!=0){       return dp[n][0]%mod;   }      if(take_one==true && dp[n][1]!=0){       return dp[n][1]%mod;   }      if(take_one==1){       return dp[n][1]= (slove(n-1, 1, 0))%mod;   }      return dp[n][0]= (slove(n-1, 1, 0)+ slove(n-1, 0, 1))%mod;}ll countStrings(int n) {     int take_zero=0;     int  take_one=0;     int count =0;     for(int i=0; i<n+3; i++){         for(int j=0; j<2; j++){             dp[i][j]=0;         }     }   ll ans=  slove(n, take_zero, take_one);   ans%=mod;    return  ans;          }};
0
mayank20213 months ago
c++ll countStrings(int n) {
   return (fibonacci(n+1)+fibonacci(n))%1000000007 ;
}ll fibonacci(ll n){    int first=1, second=1, temp;        for(int i=3; i<=n; i++)     {         temp=(first+second)%1000000007;         first=second%1000000007;         second=temp;     }     return second;    }
0
mydirtymind694 months ago
long z = 1, o = 1;
for(int i = 2; i <= n; i++) {
    long temp = z;
    z = (z + o) % mod;
    o = temp % mod;
}
return (z + o) % mod;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.