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__## Java

`class` `Subset_sum { ` ` ` `static` `int` `countStrings(` `int` `n) ` ` ` `{ ` ` ` `int` `a[] = ` `new` `int` `[n]; ` ` ` `int` `b[] = ` `new` `int` `[n]; ` ` ` `a[` `0` `] = b[` `0` `] = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) { ` ` ` `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 function */` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `System.out.println(countStrings(` `3` `)); ` ` ` `} ` `} ` `/* This code is contributed by Rajat Mishra */` |

*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:

- Java Program to Convert a Decimal Number to Binary & Count the Number of 1s
- Java Program for Print Number series without using any loop
- Java Program for Range sum queries without updates
- Java Program to Print a Semicolon Without Using Semicolon
- Java Program to Illustrate a Method Without Parameters and Return Type
- Java Program to Print any Statement without Using the Main Method
- Binary string with given frequencies of sums of consecutive pairs of characters
- Check if a binary string has two consecutive occurrences of one everywhere
- Java Program to Write an Array of Strings to the Output Console
- Java Program to Print all the Strings that Match a Given Pattern from a File
- Java Program to Count trailing zeroes in factorial of a number
- Java Program to Count the Total Number of Vowels and Consonants in a String
- Java Program to Create a Singly Linked List and Count the Number of Nodes
- How to print an Array in Java without using Loop
- Access Super Class Methods and Instance Variables Without super Keyword in Java
- Java Program for Binary Search (Recursive and Iterative)
- Java Program for Binary Insertion Sort
- Java Program for Decimal to Binary Conversion
- Java Program to Convert Integer Values into Binary
- Java Program to Convert Binary Code Into Equivalent Gray Code Using Recursion