Distinct permutations of a string containing duplicates using HashSet in Java
Given a string str that may contain duplicate characters, the task is to print all the distinct permutations of the given string such that no permutation is repeated in the output.
Input: str = “ABA”
Input: str = “ABC”
Approach: An approach to generate all the permutations of a given string has been discussed in this article. All the permutations generated by this approach can be stored in a HashSet in order to avoid duplicates.
Below is the implementation of the above approach:
ABA AAB BAA