- Related Questions & Answers
- Add minimum number to an array so that the sum becomes even in C programming
- C Program for Tower of Hanoi
- C++ Program for Dijkstra’s shortest path algorithm?
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C program to find the length of a string?
- C Program to Check if all digits of a number divide it
- C Program to Check if a Given String is a Palindrome?
- Concatenate a string given number of times in C++ programming
- Write a program to Delete a Tree in C programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C programming
- Superperfect Number in C programming
- Sum of the nodes of a Singly Linked List in C Program
- Sum of first n natural numbers in C Program

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will learn about the solution to the problem statement given below.

**Problem statement** − We are given a positive integer N, we need to count all possible distinct binary strings available with length N such that no consecutive 1’s exist in the string.

Now let’s observe the solution in the implementation below −

# count the number of strings 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] # main n=5 print("The number of strings: ",countStrings(n))

The number of strings: 13

All the variables are declared in the local scope and their references are seen in the figure above.

In this article, we have learned about how we can make a Python Program to Count number of binary strings without consecutive 1’

Advertisements