Given a string with duplicate characters allowed, print first n permutations of given string such that no permutation is repeated.
Input : string = "abcab", n = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : kkoo koko kook okko
Python provides an inbuilt method to find the permutations of any given sequence which is present in the
itertools package. But this method doesn’t provide unique permutations. Hence to ensure that any permutation is not repeated, we use a set and follow the below conditions:
- If the permutation is not present in the set, print it and insert it in the set. Increment the count of number of unique permutations.
- Else, move on to the next permutation.
Below is the implementation of the above approach:
aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb
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.
- Python - Itertools.Permutations()
- Print all distinct permutations of a given string with duplicates
- Java Program to print distinct permutations of a string
- itertools.combinations() module in Python to print all possible combinations
- Combinations in Python without using itertools
- Print distinct sorted permutations with duplicates allowed in input
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Distinct permutations of the string | Set 2
- Iterative program to generate distinct Permutations of a String
- Count of distinct permutations of every possible length of given string
- Python - Itertools.tee()
- Python Itertools
- itertools.groupby() in Python
- Python - itertools.repeat()
- Python - Itertools.takewhile()
- Python - Itertools.compress()
- Python - Itertools.dropwhile()
- Python - Itertools.cycle()
- Python - Itertools.chain()
- Python - Itertools.starmap()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.