2dbi
Home/PhonePe/All Unique Permutations of a String
PPhonePe·DSASDE-1DSA / Coding Round

All Unique Permutations of a String

Problem

Given a string s that may contain duplicate characters, return all unique permutations.

Example

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

Constraints

  • 1 ≤ s.length ≤ 8
  • Characters are uppercase English letters

Approach

Backtracking with a frequency map to avoid duplicate branches. Sort characters first, then skip if same character was already used at the same recursion depth.

Follow-up

Return permutations in lexicographic order without sorting the final output list.

added 6 days ago
LeadersAccount