Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s.

Examples:

Input: N = 2 Output: 3 // The 3 strings are 00, 01, 10 Input: N = 3 Output: 5 // The 5 strings are 000, 001, 010, 100, 101

##
Recommended: Please solve it on “__PRACTICE__ ” first, before moving on to the solution.

__PRACTICE__## Python3

`# Python program to count ` `# all distinct binary strings ` `# without two consecutive 1's ` ` ` `def` `countStrings(n): ` ` ` ` ` `a ` `=` `[` `0` `for` `i ` `in` `range` `(n)] ` ` ` `b ` `=` `[` `0` `for` `i ` `in` `range` `(n)] ` ` ` `a[` `0` `] ` `=` `b[` `0` `] ` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, n): ` ` ` `a[i] ` `=` `a[i` `-` `1` `] ` `+` `b[i` `-` `1` `] ` ` ` `b[i] ` `=` `a[i` `-` `1` `] ` ` ` ` ` `return` `a[n` `-` `1` `] ` `+` `b[n` `-` `1` `] ` ` ` `# Driver program to test ` `# above functions ` ` ` `print` `(countStrings(` `3` `)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

**Output:**

5

Please refer complete article on Count number of binary strings without consecutive 1’s for more details!

## Recommended Posts:

- Python - Ways to print longest consecutive list without considering duplicates element
- Python | Pair the consecutive character strings in a list
- Python | Merge Consecutive digits Strings
- Python - Merge consecutive empty Strings
- Python - Consecutive element deletion strings
- Python program to check if the list contains three consecutive common numbers in Python
- Python Map | Length of the Longest Consecutive 1's in Binary Representation of a given integer
- Python Program for Print Number series without using any loop
- Python program to Sort Strings by Punctuation count
- Python | Tokenizing strings in list of strings
- Python | Count K character between consecutive characters
- Python program for sum of consecutive numbers with overlapping in lists
- Binary string with given frequencies of sums of consecutive pairs of characters
- Python Program for Range sum queries without updates
- Python program to print Calendar without calendar or datetime module
- Python program to Sort a List of Strings by the Number of Unique Characters
- Check whether a number has consecutive 0's in the given base or not
- Python - Count Strings with substring String List
- Python | Program to accept the strings which contains all vowels
- Python program to find uncommon words from two Strings