Permutations of a String

Recursion · hard

Implement `permutations(str)` that returns an array of **all unique permutations** of `str` in any order. Example: `permutations('ab')` → `['ab', 'ba']`.

Hints
  • For each character, fix it at the front and recursively generate permutations of the rest.
  • Use a Set to automatically deduplicate when characters repeat.

Learning resources

Loading...

Run your code to see results.