a = [1, 2, 3] n = 3 heapPermutation(a, n, n) print() perm_iterator = itertools.permutations(a) for item in perm_iterator: print(item)
Here is what the above code is Doing:
1. It’s creating a list of all permutations of the list a.
2. It’s printing all the permutations one by one.
Note: The permutations are printed in a lexicographic sorted order.
You can learn more about itertools.permutations() here.
Time Complexity: O(n*n!) Note that there are n! permutations and it requires O(n) time to print a a permutation.
Auxiliary Space: O(n*n!) The worst case space required by the program is O(n*n!) .
This happens when all permutations are unique.