Permutation vs Combination Basic Theory
Let's break it down in a simple way with examples.
Permutation vs Combination
Both permutation and combination are ways to select items from a group, but:
- Permutation → Order matters
- Combination → Order does NOT matter
1. Permutation (Order Matters)
If you have N items and you want to pick R items where order matters
Example:
You have A, B, C, and you want to select 2 letters in different orders.
Possible selections:
AB, BA, AC, CA, BC, CB
(Different order matters!)
2. Combination (Order Doesn’t Matter)
If you have N items and you want to pick R items where order does NOT matter
Example:
You have A, B, C, and you want to select 2 letters, but order doesn’t matter.
Possible selections:
AB, AC, BC
(Here, AB
is the same as BA
)
How This Applies to LeetCode Coding Problems
-
Use Permutations when order matters
- Example: Generating all possible orderings of a list (
[1,2,3]
→[1,2,3], [1,3,2], [2,1,3]
etc.) - LeetCode problem: "Permutations" (Backtracking)
- Example: Generating all possible orderings of a list (
-
Use Combinations when order does NOT matter
- Example: Picking subsets (
[1,2,3]
→{1,2}, {1,3}, {2,3}
) - LeetCode problem: "Combinations" (Backtracking)
- Example: Picking subsets (
When to Use Which?
- Use permutations when solving problems like arranging numbers or letters in different orders.
- Use combinations when selecting elements without worrying about order.
Quick Mnemonic
👉 Permutation = Position matters
👉 Combination = Choice matters, but order doesn't