Word Search II

IF
AlgoAxiomStaff Engineers
JSTS
Hard20 mins

Given an m x n board of characters and a list of strings words, return all words on the board.

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.

Examples

Example 1:

Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]

Output: ["eat","oath"]

Explanation: The words "eat" and "oath" can be formed by traversing adjacent cells on the board. "pea" and "rain" cannot be formed.

Example 2:

Input: board = [["a","b"],["c","d"]], words = ["abcb"]

Output: []

Explanation: The word "abcb" would require revisiting cell (0,1) which is not allowed.

Constraints

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 12
  • board[i][j] is a lowercase English letter
  • 1 <= words.length <= 3 * 10⁴
  • 1 <= words[i].length <= 10
  • words[i] consists of lowercase English letters
  • All strings in words are unique
Source: Backtracking pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle