# Count number of binary strings of length N having only 0’s and 1’s

Given an integer **N**, the task is to count the number of binary strings of length N having only 0’s and 1’s.

**Note:** Since the count can be very large, return the answer modulo 10^9+7.

**Examples:**

Input:2

Output:4

Explantion:The numbers are 00, 01, 11, 10. Hence the count is 4.

Input:3

Output:8

Explantion:The numbers are 000, 001, 011, 010, 111, 101, 110, 100. Hence the count is 8.

**Approach:** The problem can be easily solved by using Permutation and Combination. At each position of the string there can only be two possibilities, i.e., 0 or 1. Therefore, the total number of permutation of 0 and 1 in a string of length N is given by 2*2*2*…(N times), i.e., 2^N. The answer can be very large, hence modulo by 10^9+7 is returned.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long ` `#define mod (ll)(1e9 + 7) ` ` ` `// Iterative Function to calculate (x^y)%p in O(log y) ` `ll power(ll x, ll y, ll p) ` `{ ` ` ` `ll res = 1; ` `// Initialize result ` ` ` ` ` `x = x % p; ` `// Update x if it is more than or ` ` ` `// equal to p ` ` ` ` ` `while` `(y > 0) { ` ` ` ` ` `// If y is odd, multiply x with result ` ` ` `if` `(y & 1) ` ` ` `res = (res * x) % p; ` ` ` ` ` `// y must be even now ` ` ` `y = y >> 1; ` `// y = y/2 ` ` ` `x = (x * x) % p; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to count the number of binary ` `// strings of length N having only 0's and 1's ` `ll findCount(ll N) ` `{ ` ` ` `int` `count = power(2, N, mod); ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll N = 25; ` ` ` ` ` `cout << findCount(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `mod = (` `int` `) (1e9 + ` `7` `); ` ` ` `// Iterative Function to calculate (x^y)%p in O(log y) ` `static` `int` `power(` `int` `x, ` `int` `y, ` `int` `p) ` `{ ` ` ` `int` `res = ` `1` `; ` `// Initialize result ` ` ` ` ` `x = x % p; ` `// Update x if it is more than or ` ` ` `// equal to p ` ` ` ` ` `while` `(y > ` `0` `) ` ` ` `{ ` ` ` ` ` `// If y is odd, multiply x with result ` ` ` `if` `((y & ` `1` `)==` `1` `) ` ` ` `res = (res * x) % p; ` ` ` ` ` `// y must be even now ` ` ` `y = y >> ` `1` `; ` `// y = y/2 ` ` ` `x = (x * x) % p; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to count the number of binary ` `// strings of length N having only 0's and 1's ` `static` `int` `findCount(` `int` `N) ` `{ ` ` ` `int` `count = power(` `2` `, N, mod); ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `25` `; ` ` ` `System.out.println(findCount(N)); ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` `mod ` `=` `1000000007` ` ` `# Iterative Function to calculate (x^y)%p in O(log y) ` `def` `power(x, y, p): ` ` ` `res ` `=` `1` `# Initialize result ` ` ` ` ` `x ` `=` `x ` `%` `p ` `# Update x if it is more than or ` ` ` `# equal to p ` ` ` ` ` `while` `(y > ` `0` `): ` ` ` ` ` `# If y is odd, multiply x with result ` ` ` `if` `(y & ` `1` `): ` ` ` `res ` `=` `(res ` `*` `x) ` `%` `p ` ` ` ` ` `# y must be even now ` ` ` `y ` `=` `y >> ` `1` `# y = y/2 ` ` ` `x ` `=` `(x ` `*` `x) ` `%` `p ` ` ` ` ` `return` `res ` ` ` `# Function to count the number of binary ` `# strings of length N having only 0's and 1's ` `def` `findCount(N): ` ` ` `count ` `=` `power(` `2` `, N, mod) ` ` ` `return` `count ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `25` ` ` `print` `(findCount(N)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `mod = (` `int` `) (1e9 + 7); ` ` ` ` ` `// Iterative Function to calculate (x^y)%p in O(log y) ` ` ` `static` `int` `power(` `int` `x, ` `int` `y, ` `int` `p) ` ` ` `{ ` ` ` `int` `res = 1; ` `// Initialize result ` ` ` ` ` `x = x % p; ` `// Update x if it is more than or ` ` ` `// equal to p ` ` ` ` ` `while` `(y > 0) ` ` ` `{ ` ` ` ` ` `// If y is odd, multiply x with result ` ` ` `if` `((y & 1) == 1) ` ` ` `res = (res * x) % p; ` ` ` ` ` `// y must be even now ` ` ` `y = y >> 1; ` `// y = y/2 ` ` ` `x = (x * x) % p; ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Function to count the number of binary ` ` ` `// strings of length N having only 0's and 1's ` ` ` `static` `int` `findCount(` `int` `N) ` ` ` `{ ` ` ` `int` `count = power(2, N, mod); ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 25; ` ` ` `Console.WriteLine(findCount(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Iterative Function to calculate ` `// (x^y)%p in O(log y) ` `function` `power(` `$x` `, ` `$y` `) ` `{ ` ` ` `$p` `= 1000000007; ` ` ` `$res` `= 1; ` `// Initialize result ` ` ` ` ` `$x` `= ` `$x` `% ` `$p` `; ` `// Update x if it is more ` ` ` `// than or equal to p ` ` ` ` ` `while` `(` `$y` `> 0) ` ` ` `{ ` ` ` ` ` `// If y is odd, multiply x with result ` ` ` `if` `(` `$y` `& 1) ` ` ` `$res` `= (` `$res` `* ` `$x` `) % ` `$p` `; ` ` ` ` ` `// y must be even now ` ` ` `$y` `= ` `$y` `>> 1; ` `// y = y/2 ` ` ` `$x` `= (` `$x` `* ` `$x` `) % ` `$p` `; ` ` ` `} ` ` ` `return` `$res` `; ` `} ` ` ` `// Function to count the number of binary ` `// strings of length N having only 0's and 1's ` `function` `findCount(` `$N` `) ` `{ ` ` ` `$count` `= power(2, ` `$N` `); ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$N` `= 25; ` ` ` `echo` `findCount(` `$N` `); ` ` ` `// This code is contributed by Rajput-Ji ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

33554432

## Recommended Posts:

- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Count binary strings with twice zeros in first half
- Number of strings of length N with no palindromic sub string
- Count number of distinct substrings of a given length
- Count the number of subsequences of length k having equal LCM and HCF
- Number of Binary Trees for given Preorder Sequence length
- Count number of strings (made of R, G and B) using given combination
- Count number of trailing zeros in Binary representation of a number using Bitset
- All possible strings of any length that can be formed from a given string
- Print all possible strings of length k that can be formed from a set of n characters
- Count unique subsequences of length K
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Check if a binary string contains all permutations of length k
- Program to add two binary strings

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.