Skip to main content

LeetCode Patterns

Subsets Pattern

The Subsets pattern enumerates every combination, permutation, or arrangement of a small set of elements by either iteratively extending an existing list of subsets with each new element or by recursing through a decision tree (include / exclude). It powers backtracking problems on combinations, permutations, and string letter cases in O(n * 2^n) time and O(n * 2^n) space.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Complexity

Time
O(n * 2^n)
Space
O(n * 2^n)

When to use Subsets

  • The problem asks for all subsets, all permutations, or all combinations of a small input.
  • You need to generate every possible string that satisfies a structural constraint (parentheses, IP addresses, letter case).
  • You can frame the answer as a sequence of include/exclude decisions on each input element.
  • The input size is small (n <= 20 for subsets, n <= 10 for permutations) because the output is exponential.
  • Counting alone is not enough — you must enumerate the actual combinations or paths.

Template

function subsets(nums) {
  const result = [[]];
  for (const num of nums) {
    const size = result.length;
    for (let i = 0; i < size; i++) {
      result.push([...result[i], num]);
    }
  }
  return result;
}

Example LeetCode problems

#ProblemDifficultyFrequency
78Subsetsmediumfoundational
90Subsets IImediumfrequently asked
46Permutationsmediumfoundational
47Permutations IImediumfrequently asked
77Combinationsmediumfoundational
39Combination Summediumcompany favorite
784Letter Case Permutationmediumclassic
22Generate Parenthesesmediumcompany favorite

Common mistakes

  • Iterating over `result` with a normal length read in the inner loop, which grows the array as you append and creates an infinite loop.
  • Forgetting to make a shallow copy of the partial subset before pushing it into the result, which means every saved subset points to the same mutating array.
  • On Subsets II, failing to sort the input first and skip duplicates — duplicates produce identical subsets that violate the unique-output requirement.
  • Using the recursive include/exclude template without a depth parameter, causing the recursion to revisit indices.
  • Pruning too aggressively in Combination Sum and missing valid combinations that use the same number multiple times.

Frequently asked questions

What is the difference between subsets, permutations, and combinations?

Subsets ignore order and any element can be present or absent (2^n total). Permutations care about order (n! total). Combinations pick k of n without regard to order (n choose k total).

When should I use iteration vs recursion for subsets?

Iteration is cleaner for the basic Subsets problem because there's no branching decision tree to express. Recursion is clearer for permutations, combination-sum, and any variant that prunes branches early.

Why is the time complexity O(n * 2^n) and not O(2^n)?

There are 2^n subsets, but copying each one into the result takes O(n) time in the worst case. Multiplying gives O(n * 2^n) for both time and output size.

How do I avoid duplicate subsets when the input has repeats?

Sort the input first so duplicates are adjacent. When generating subsets, only extend previously-added subsets if the current element differs from the previous, or only extend the subsets added in the last iteration if the previous element was a duplicate.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill the Subsets pattern under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →