Skip to content

Word Search II

HardTrees
Decode it first. Clues, constraints, pattern and plan before any code.

You are given an m x n grid of letters board and a list of words. Return every word from the list that can be spelled on the board.

A word is spelled by starting on any cell and moving to a horizontally or vertically adjacent cell for each next letter. The same cell may not be used twice within one word. Return each found word once, in any order.

board = [["o", "a", "a", "n"],
         ["e", "t", "a", "e"],
         ["i", "h", "k", "r"],
         ["i", "f", "l", "v"]]
words = ["oath", "pea", "eat", "rain"]       →  ["eat", "oath"]
 
board = [["a", "b"], ["c", "d"]], words = ["abcb"]  →  []

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^4
  • 1 <= words[i].length <= 10
  • All words in words are unique.