Print all distinct permutation of a string having duplicates.
Input : ABCA Output : AABC AACB ABAC ABCA ACBA ACAB BAAC BACA BCAA CABA CAAB CBAA
An algorithm to print all distinct permutations has already been discussed here. Here we’ll discuss one more approach to do the same. Recall first how we print permutations without any duplicates in the input string. It is given here. Let’s now take the case of the string “ABAC”. While generating permutations, let’s say we are at index = 0, swap it with all elements after it. When we reach at i=2, we see that in the string s[index…i-1], there was an index which is equal to s[i]. Thus, swapping it will produce repeated permutations. Thus, we don’t swap it. The below explains it better.
Illustration: Let us understand with below example.
i = 0 1 2 3 A B A C index = 0, s = A Start swapping s[index] with s[i] following it: i = index + 1 = 1 Since s[index] != s[i], swap and recur. i = 2, s[index] == s[i], don't swap i = 3, s[index] != s[i], swap and recur.
Below code does the same.
ABCA ABAC ACBA ACAB AACB AABC BACA BAAC BCAA CBAA CABA CAAB
Generate all distinct strings is to simply use some if conditions. The technique above uses an extra loop inside the recursion which causes a major time complexity cost. Instead, we can improve it by little pre-processing. We first sort the given string and then apply the below code.
Below is the implementation of the above idea:
aabc aacb abac abca acba acab baac baca bcaa caba caab cbaa Total distinct permutations = 12
Time complexity: If we take the length of string to be N, then the complexity of my code will be O(N log N) for sorting and O(N*N!) for the permutation. Total time complexity would be O(N log N + N*N!) which is effectively only O(N*N!).
This article is contributed by ekta1994.
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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Print all distinct permutations of a given string with duplicates
- Print first n distinct permutations of string using itertools in Python
- Java Program to print distinct permutations of a string
- Iterative program to generate distinct Permutations of a String
- Distinct permutations of a string containing duplicates using HashSet in Java
- Count of distinct permutations of every possible length of given string
- Count of distinct permutations of length N having no similar adjacent characters
- Print distinct sorted permutations with duplicates allowed in input
- Find distinct characters in distinct substrings of a string
- Find Kth largest string from the permutations of the string with two characters
- Write a program to print all permutations of a given string
- Print all palindrome permutations of a string
- Permutations of a given string using STL
- All permutations of a string using iteration
- Print all the palindromic permutations of given string in alphabetic order
- Time complexity of all permutations of a string
- Check if given string can be formed by two other strings or their permutations
- Number of unique permutations starting with 1 of a Binary String
- Number of permutations of a string in which all the occurrences of a given character occurs together