VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-of-unique-permutations-starting-with-1-of-a-binary-string/

⇱ Number of unique permutations starting with 1 of a Binary String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Number of unique permutations starting with 1 of a Binary String

Last Updated : 6 Apr, 2023

Given a binary string composed of 0's and 1's. The task is to find the number of unique permutation of the string which starts with 1. 
Note: Since the answer can be very large, print the answer under modulo 109 + 7.
Examples: 
 

Input : str ="10101001001"
Output : 210

Input : str ="101110011"
Output : 56


 


The idea is to first find the count of 1's and the count of 0's in the given string. Now let us consider that the string is of length and the string consists of at least one 1. Let the number of 1's be and the number of 0's be . Out of n number of 1's we have to place one 1 at the beginning of the string so we have n-1 1's left and m 0's we have to permute these (n-1) 1's and m 0's in length (L-1) of the string. 
Therefore, the number of permutation will be: 
 

(L-1)! / ((n-1)!*(m)!)


Below is the implementation of the above idea: 
 


Output: 
210

 

Time Complexity: O(n), where n is the length of the string.

Auxilitary Space Complexity : O(n)
 

Comment