Backtracking
3 programs — patterns, complexity, and code
| # | Program Name | Pattern | Time | Space | Key Idea | Level |
|---|---|---|---|---|---|---|
| 1 | subsets | Backtracking | O(2ⁿ) | O(n) | At each index choose to include or skip the element, recurse, then backtrack. | Middle |
| 2 | permutations | Backtracking | O(n!) | O(n) | Swap each element into the current position, recurse on the rest, then swap back. | Middle |
| 3 | wordSearch | DFS + Backtracking | O(m·n·4ᴸ) | O(L) | DFS from each cell; mark visited temporarily and restore on backtrack. | Complex |