Get AI summaries of any video or article — Sign up free
Programming Terms: Combinations and Permutations thumbnail

Programming Terms: Combinations and Permutations

Corey Schafer·
4 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Use combinations when the order of selected items does not create a new outcome; treat rearrangements as equivalent.

Briefing

Combinations and permutations differ in one crucial way: whether the order of selected items matters. A combination counts groups where rearranging the same values doesn’t create a new result; a permutation counts every distinct ordering as a separate outcome. That distinction shows up constantly in computer science, from search and constraint problems to word and pattern matching.

Using Python’s iter tools module, the examples start with a list [1, 2, 3] and groups of size 3. With combinations, only one group appears—[1, 2, 3]—because any other ordering like [3, 2, 1] represents the same set of values when order is ignored. When the group size drops to 2, combinations produce three groups: [1, 2], [1, 3], and [2, 3]. Notably, the reverse orderings (like [2, 1]) don’t appear because they don’t count as new combinations.

Switching to permutations with the same list and group size 2 changes the output. The same value pairs now appear in both orders: [1, 2] and [2, 1], [1, 3] and [3, 1], [2, 3] and [3, 2]. The key takeaway is that permutations treat different orderings as distinct, while combinations treat them as equivalent.

The practical importance comes down to how a problem is worded and what “counts” as a different answer. For a combinations-friendly scenario, the transcript uses a list of six numbers [1, 2, 3, 4, 5, 6] and asks for groups of three whose sum equals 10. With combinations, the results are limited to sets of values that add to 10, such as [1, 3, 6] (sum 10), [1, 4, 5] (sum 10), and [2, 3, 5] (sum 10). The ordering doesn’t matter because the question only cares about which numbers are included, not the sequence.

Replacing combinations with permutations for the same “sum equals 10” task produces many more results, including cases like [6, 3, 1] that are effectively the same as [1, 3, 6] under an order-insensitive interpretation. That mismatch illustrates why combinations are usually the right tool when the task is about selecting values that satisfy a condition.

For a permutations-friendly scenario, the transcript builds a simple word-matching game. Given a sample word, it checks whether any arrangement of its letters can form the target word. Here, combinations fail because they collapse different orderings into one group, missing valid rearrangements. Permutations succeed because they enumerate every possible ordering of the letters; once the correct sequence appears, the joined letters match the target word.

Finally, the transcript flags a performance concern: permutations grow extremely fast as the number of items increases, which can quickly consume computing power. The practical rule is straightforward—use combinations when order doesn’t matter, and use permutations when order is part of the answer.

Cornell Notes

Combinations count groups of items where order doesn’t matter; permutations count arrangements where order does matter. In the examples, combinations of [1,2,3] taken two at a time yield [1,2], [1,3], and [2,3], while permutations also include reversed orders like [2,1] and [3,1]. A sum-based constraint problem (groups of three that add to 10) fits combinations because [1,3,6] and [6,3,1] represent the same selected values. A word-matching task fits permutations because only the correct ordering of letters produces a match. Permutations can also explode in size as more items are added, so they’re computationally heavier.

How can you tell whether a problem should use combinations or permutations?

Look at whether the order of selected items changes the meaning of a solution. If [a,b,c] and [c,b,a] should be treated as the same outcome, use combinations. If different orderings represent different valid answers (like letter sequences in a word), use permutations.

Why do combinations produce fewer results than permutations in the examples?

Combinations treat any reordering of the same values as identical. For instance, with values {1,2,3} and group size 2, combinations list [1,2], [1,3], [2,3] but not [2,1] or [3,1] because those are just the same value sets in a different order.

What does the “sum equals 10” example demonstrate about combinations?

It demonstrates that when the task depends only on which numbers are included (not their order), combinations are the right choice. From [1,2,3,4,5,6], combinations of three numbers that sum to 10 include [1,3,6], [1,4,5], and [2,3,5]. Using permutations adds extra sequences like [6,3,1], which are redundant under an order-insensitive interpretation.

Why does the word-matching game require permutations?

Because the target word depends on the exact order of letters. Combinations collapse different orderings into one group, so they miss valid rearrangements. Permutations enumerate every ordering of the letters, allowing the code to find an arrangement whose joined letters match the sample word.

What computational risk comes with permutations?

Permutations grow extremely quickly as the number of items increases. That rapid growth can multiply the number of candidate arrangements and consume significant computing power, so permutations should be used only when order truly matters.

Review Questions

  1. Given a list of numbers and a requirement like “choose 3 numbers whose sum is 12,” which should be used—combinations or permutations—and why?
  2. If a task asks whether any arrangement of letters can form a specific word, what method fits best and what failure mode occurs with combinations?
  3. How does the output change when you switch from combinations to permutations for the same list and group size?

Key Points

  1. 1

    Use combinations when the order of selected items does not create a new outcome; treat rearrangements as equivalent.

  2. 2

    Use permutations when the order matters and different sequences count as distinct answers.

  3. 3

    A sum-based constraint problem typically matches combinations because only the selected values matter, not their arrangement.

  4. 4

    A word or sequence-matching problem typically matches permutations because the exact ordering determines success.

  5. 5

    Permutations can increase output size dramatically as the number of items grows, which can strain computation.

  6. 6

    Python’s iter tools module provides built-in combinations and permutations so programmers don’t need to implement them from scratch.

Highlights

Combinations count value groups where [1,2,3] is the same as [3,2,1]; permutations treat them as different.
For “groups of three that sum to 10,” combinations yield a small set like [1,3,6], [1,4,5], and [2,3,5], while permutations add redundant reorderings.
A letter-arrangement word game fails with combinations but succeeds with permutations because only the correct ordering matches the target word.
Permutations can explode in number as more letters/numbers are included, making them computationally expensive.

Topics

  • Combinations vs Permutations
  • Order Matters
  • Python Itertools
  • Sum Constraints
  • Word Matching