Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. A permutation should not have repeated strings in the output.
Input: str = “aa”
Note that “aa” will be printed only once
as duplicates are not allowed.
Input: str = “ab”
Approach: Write a recursive function that removes a character one by one from the original string and generates a new string by appending these removed characters. The base condition will be when all the characters have been used. In that case, insert the generated string (a permutation of the original string) in a set in order to avoid duplicates.
Below is the implementation of the above approach: