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.
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 w 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:
Time Complexity: O(n), where n is the length of the string.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if a binary string contains all permutations of length k
- Count of cyclic permutations having XOR with other binary string as 0
- Check if an encoding represents a unique binary string
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Permutations of a given string using STL
- All permutations of a string using iteration
- Distinct permutations of the string | Set 2
- Print all palindrome permutations of a string
- Permutations of string such that no two vowels are adjacent
- Print all permutations of a string in Java
- Number of subsequences in a given binary string divisible by 2
- Number of sub-strings in a given binary string divisible by 2
- Number of substrings with odd decimal value in a binary string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Print all distinct permutations of a given string with duplicates
- Generate all permutations of a string that follow given constraints
- Print all lexicographical greater permutations of a given string
- Write a program to print all permutations of a given string
- Number of sub-sequences of non-zero length of a binary string divisible by 3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.