| 
 | 1 | +"""  | 
 | 2 | +Problem Link: https://practice.geeksforgeeks.org/problems/consecutive-1s-not-allowed1912/1  | 
 | 3 | + | 
 | 4 | +Input:  | 
 | 5 | +N = 3  | 
 | 6 | +Output: 5  | 
 | 7 | +Explanation: 5 strings are (000,  | 
 | 8 | +001, 010, 100, 101).  | 
 | 9 | + | 
 | 10 | +Example 2:  | 
 | 11 | +Input:  | 
 | 12 | +N = 2  | 
 | 13 | +Output: 3  | 
 | 14 | +Explanation: 3 strings are  | 
 | 15 | +(00,01,10).  | 
 | 16 | + | 
 | 17 | +Your Task:  | 
 | 18 | +Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer.   | 
 | 19 | +You don't to print answer or take inputs.   | 
 | 20 | + | 
 | 21 | +Expected Time Complexity: O(N)  | 
 | 22 | +Expected Auxiliary Space: O(N)  | 
 | 23 | + | 
 | 24 | +Constraints:  | 
 | 25 | +1 ≤ N ≤ 105  | 
 | 26 | +"""  | 
 | 27 | +class Solution:  | 
 | 28 | + | 
 | 29 | +	def countStrings(self,n):  | 
 | 30 | +	 zero_count = 1  | 
 | 31 | +	 one_count = 1  | 
 | 32 | + | 
 | 33 | +	 for i in range(1, n):  | 
 | 34 | +	 zero_count, one_count = (zero_count + one_count) % 1000000007, zero_count  | 
 | 35 | + | 
 | 36 | +	 return (zero_count + one_count) % 1000000007  | 
 | 37 | + | 
 | 38 | + | 
 | 39 | +# TLE  | 
 | 40 | +class Solution1:  | 
 | 41 | + | 
 | 42 | + def countStrings(self,n):  | 
 | 43 | + return len(self.helper(n))  | 
 | 44 | + | 
 | 45 | + def helper(self, n):  | 
 | 46 | + if n == 1:  | 
 | 47 | + return ["0", "1"]  | 
 | 48 | + numbers = self.helper(n-1)  | 
 | 49 | + res = []  | 
 | 50 | + for num in numbers:  | 
 | 51 | + res.append(num + "0")  | 
 | 52 | + if num[-1] != "1":  | 
 | 53 | + res.append(num + "1")  | 
 | 54 | + return res  | 
0 commit comments