Problems

Word Search

medium
medium
matrix
backtracking
depth-first-search

Return whether word can be formed from horizontally or vertically adjacent board cells without reusing a cell.

Constraints

  • 1 <= rows, columns <= 6
  • 1 <= word.length <= 15
  • Board and word contain letters

Follow-up

Which frequency or prefix checks can reject impossible searches early?

Examples

Example 1

Input: board = ["ABCE","SFCS","ADEE"], word = "ABCCED"
Output: true

Example 2

Input: board = ["ABCE","SFCS","ADEE"], word = "SEE"
Output: true

Example 3

Input: board = ["ABCE","SFCS","ADEE"], word = "ABCB"
Output: false
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.