780. Count Binary Strings Without Consecutive 1s (Modulo 10^9+7)

MediumRecursionRecursion

Given a non-negative integer n, return the number of binary strings of length n that do NOT contain two consecutive '1' characters, taken modulo 10^9 + 7. If n == 0, return 1 (the empty string counts). Implement with recursion + memoization.

Input: A single non-negative integer n.

Output: Return an integer count mod 10^9 + 7.

Examples

Example 1
Input: 3
Output: 5
Explanation: 000, 001, 010, 100, 101.
Example 2
Input: 1
Output: 2
Explanation: 0, 1.
Example 3
Input: 0
Output: 1
Explanation: Empty string.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →