HARD NC#63 Blind #73 Backtracking / Trie

212. Word Search II

📖 Problem

Given an m x n board of characters and a list of strings words, return all words on the board that are present in the list. Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • Array methods (`push`, `pop`, `shift`, `slice`)
  • Object/Map/Set usage patterns
  • Function parameter and return typing
  • Recursive stack flow
  • Cycle prevention
  • Post-order reasoning

Logical Thinking Concepts

  • Define invariants before coding
  • Check edge cases first (`[]`, single element, duplicates)
  • Estimate time/space before implementation
  • Apply DFS reasoning pattern
  • Apply Backtracking reasoning pattern

💡 Approach

  • Build trie from words for efficient prefix checking
  • Use backtracking to find all valid words starting from each cell
  • Track visited cells to avoid reuse
  • Time: O(m * n * 4^L) worst case, where L is max word length

🛠️ Hints & Pitfalls

Hints

  • Build trie from words for efficient prefix checking
  • Use backtracking to find all valid words starting from each cell
  • Track visited cells to avoid reuse

Common Pitfalls

  • Time: O(m * n * 4^L) worst case, where L is max word length

🧪 Test Cases

Hidden tests on submit: 3

Test Case 1
Not run
Input:
dfs(r: number, c: number, parent: TrieNode | null);
Expected:
Computed from hidden reference
Test Case 2
Not run
Input:
dfs(r + 1, c, curr);
Expected:
Computed from hidden reference
Test Case 3
Not run
Input:
dfs(r - 1, c, curr);
Expected:
Computed from hidden reference

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit