00:00

Problem Statement

Input/Output Examples

Visual Explanation

The rule chars[i] == chars[i - 1] && !used[i - 1] makes sure equal characters are always used in their original order. That removes every duplicate branch before it is explored.

Approaches

Both approaches produce the distinct permutations in sorted order. One recurses; the other steps from one permutation to the next.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the length of the string. There can be up to n! permutations, each of length n, so any solution that lists them all needs at least O(n! × n) time.

Interview Tips and Recommended Approach

  • Ask about duplicates. Using a Set to remove them works but still generates every duplicate first.
  • Explain the skip rule. It is the part interviewers probe most often.
  • Know the output size. 10 characters already give 3,628,800 permutations.
  • Mention nextPermutation. It is a classic algorithm and useful when you need permutations one at a time.

Recommended Approach: Backtracking With Duplicate Skip

Why: it shows the general backtracking pattern, removes duplicates without a set and keeps only the current path in memory.