LeetCode 46 Permutations - Solution

The core idea

Use backtracking. Build the permutation one position at a time.

At each position, try every unused element. Mark the element as used, recurse to fill the next position, then unmark it (backtrack).

When the permutation reaches length nn, add it to results.

The "used" tracking ensures no element appears twice. Backtracking ensures you explore all possibilities.