🔄 Subsets & Permutations

Generate All Combinations

Day 6/6 Pattern: Backtracking

📖 Pattern Overview

🎯 When to Use

  • Generate all subsets (power set)
  • Generate all permutations
  • Combination problems
  • "All possible" questions

⚡ Complexity

  • Subsets: O(2^n)
  • Permutations: O(n!)
  • Combinations: O(C(n,k))
  • Grows: Exponentially!

🔑 How It Works

  • Include/Exclude: Each element
  • Backtrack: Explore all paths
  • Decision Tree: 2^n leaves
  • Collect: All solutions

🎯 Select Problem

Generate All Subsets

Normal
Array Size (n): 0
Total Results: 0
Expected: -
Status: Ready

Generated Results:

Current Step:

Click "Generate" to start

📚 LeetCode Problems

78. Subsets

Medium

Generate all possible subsets (power set).

Solve on LeetCode →

46. Permutations

Medium

Generate all possible permutations.

Solve on LeetCode →

77. Combinations

Medium

Return all combinations of k numbers from 1 to n.

Solve on LeetCode →

39. Combination Sum

Medium

Find all combinations that sum to target.

Solve on LeetCode →

90. Subsets II

Medium

Subsets with duplicates (no duplicate subsets).

Solve on LeetCode →

47. Permutations II

Medium

Permutations with duplicates.

Solve on LeetCode →

🧠 Algorithm Deep Dive

Subsets (Power Set)

Every element has 2 choices: include or exclude!

function subsets(nums) {
    const result = [];
    
    function backtrack(index, current) {
        // Base case: processed all elements
        if (index === nums.length) {
            result.push([...current]);
            return;
        }
        
        // Choice 1: Exclude current element
        backtrack(index + 1, current);
        
        // Choice 2: Include current element
        current.push(nums[index]);
        backtrack(index + 1, current);
        current.pop();
    }
    
    backtrack(0, []);
    return result;
}

// For [1,2,3]: 2^3 = 8 subsets
// [], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]

Permutations

Try each remaining element in each position!

function permute(nums) {
    const result = [];
    
    function backtrack(current, remaining) {
        if (remaining.length === 0) {
            result.push([...current]);
            return;
        }
        
        for (let i = 0; i < remaining.length; i++) {
            // Choose
            current.push(remaining[i]);
            
            // Explore
            const newRemaining = [
                ...remaining.slice(0, i),
                ...remaining.slice(i + 1)
            ];
            backtrack(current, newRemaining);
            
            // Unchoose
            current.pop();
        }
    }
    
    backtrack([], nums);
    return result;
}

// For [1,2,3]: 3! = 6 permutations
// [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]

Combinations

Choose k elements, order doesn't matter!

function combine(n, k) {
    const result = [];
    
    function backtrack(start, current) {
        if (current.length === k) {
            result.push([...current]);
            return;
        }
        
        for (let i = start; i <= n; i++) {
            current.push(i);
            backtrack(i + 1, current);
            current.pop();
        }
    }
    
    backtrack(1, []);
    return result;
}

// For n=4, k=2: C(4,2) = 6 combinations
// [1,2], [1,3], [1,4], [2,3], [2,4], [3,4]

Real-World Applications

  • Password Generation: All possible combinations
  • Test Case Generation: All input combinations
  • Feature Flags: All on/off combinations
  • Menu Selection: All meal combinations
  • A/B Testing: All variant combinations
  • Cryptography: Key generation, brute force