Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Unique Permutations

Medium Acceptance 46.50% Points 30.00

Given a string s, the goal is to return a list of all unique permutations of the string, including those that may contain duplicate characters, and return them in lexicographical order.

Examples
Example 1

Example 1

Input: "AAB"

Output: ["AAB", "ABA", "BAA"]

Explanation: The string "AAB" has 3 unique permutations due to duplicate characters: "AAB", "ABA", and "BAA".

Example 2

Example 2

Input: "ABBA"

Output: ["AABB", "ABAB", "ABBA", "BAAB", "BABA", "BBAA"]

Explanation: The string "ABBA" has 6 unique permutations, accounting for duplicate characters.

Hints
Hint 1
Expected Time Complexity: O(n! * n)
Hint 2
Expected Space Complexity: O(n! * n)
Constraints
  • 1 <= s.size() <= 5
Companies
Amazon Microsoft Samsung Walmart Cisco + 1 more
Topics
String
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions