750. Generate All Unique Permutations of a String

MediumRecursionRecursion

Given a string s, return a list containing every DISTINCT permutation of its characters (i.e., duplicates are suppressed). The output list must be sorted lexicographically. If s is empty, return a list with one element: an empty string.

Input: A string s enclosed in double quotes.

Output: Return a lexicographically sorted list of strings as ["...","...",...].

Examples

Example 1
Input: "abc"
Output: ["abc","acb","bac","bca","cab","cba"]
Explanation: All 6 permutations in lex order.
Example 2
Input: "aab"
Output: ["aab","aba","baa"]
Explanation: Only 3 distinct permutations because two 'a's are indistinguishable.
Example 3
Input: "a"
Output: ["a"]
Explanation: Single-character string has one permutation.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →