Given a string that may contain duplicates, write a function to print all permutations of given string such that no permutation is repeated in output.
Input: str = "AB" Output: AB BA Input: str = "AA" Output: AA Input: str = "ABC" Output: ABC ACB BAC BCA CBA CAB Input: str = "ABA" Output: ABA AAB BAA Input: str = "ABCA" Output: AABC AACB ABAC ABCA ACBA ACAB BAAC BACA BCAA CABA CAAB CBAA
We have discussed an algorithm to print all permutations in below post. It is strongly recommended to refer below post as a prerequisite of this post.
The algorithm discussed on above link doesn’t handle duplicates.
1 ABCC 2 ACBC 3 ACCB 4 BACC 5 BCAC 6 BCCA 7 CABC 8 CACB 9 CBAC 10 CBCA 11 CCAB 12 CCBA
The above code is taken from a comment below by Mr. Lazy.
Time Complexity: O(n2 * n!)
Auxiliary Space: O(1)
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.
- Print distinct sorted permutations with duplicates allowed in input
- Distinct permutations of a string containing duplicates using HashSet in Java
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Print first n distinct permutations of string using itertools in Python
- Java Program to print distinct permutations of a string
- Count of distinct permutations of every possible length of given string
- Write a program to print all permutations of a given string
- Print all the palindromic permutations of given string in alphabetic order
- Print all lexicographical greater permutations of a given string
- Iterative program to generate distinct Permutations of a String
- Distinct permutations of the string | Set 2
- Print all palindrome permutations of a string
- Print all permutations of a string in Java
- Count of distinct permutations of length N having no similar adjacent characters
- Print all permutations with repetition of characters
- Print all permutations in sorted (lexicographic) order
- Iterative approach to print all permutations of an Array
- Print all permutations of a number N greater than itself
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Generate all permutations of a string that follow given constraints