Back to Solutions

Creating Strings

Explanation

In this problem, we are given a string and our task is to print all the unique permutations of the string in alphabetical order.

It can be done using the built-in permutations function in Python or next_permutation function in C++ and storing the result in a set to remove duplicates.

Code

import itertools

ans = sorted(set(map(lambda e: "".join(e),itertools.permutations(input()))))
print(len(ans), *ans, sep="\n")