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
- Print all distinct permutations of a given string with duplicates
- Java Program to print distinct permutations of a string
- Python - Itertools.Permutations()
- Print distinct sorted permutations with duplicates allowed in input
- Distinct permutations of the string | Set 2
- Iterative program to generate distinct Permutations of a String
- Write a program to print all permutations of a given string
- Print all the palindromic permutations of given string in alphabetic order
- itertools.combinations() module in Python to print all possible combinations
- Python | All Permutations of a string in lexicographical order without using recursion
- Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
- Python Itertools
- Python - Itertools.cycle()
- Python - Itertools.dropwhile()
- Python - Itertools.zip_longest()