Data Structures & Algorithms Backtracking
💡
Exercise 145

Permutations 20 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Given an array of distinct integers, return all permutations. Order matters — [1,2,3] and [3,2,1] are different.

Backtracking: at each position, try every unused number. n! total permutations.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Find all permutations of [1,2,3]. Print the count.
Use a 'used' set or check if num in path. For each unused num, add to path, recurse, remove.
🧪 Test Cases
Input
len(permute([1, 2, 3]))
Expected
6
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.