Word Search II

Hard#212trie and grid together

There is a grid of letters and a list of words. Return every word that can be spelled by a path of cells touching up, down, left or right, with no cell used twice in one word. Our example: the grid tea / ons and the words toe, tea, ten — the answer is [ten, tea]; "toe" is not on the board.

input
board: a grid of letters, m rows of n each; words: words, all different.
returns
Every word of words that can be read along a path of cells, each next one beside the one before it (above, below, left or right) and no cell used twice in the word. Each word once, in any order; [] if there is none.
constraints
  • 1 ≤ m = board.length ≤ 12
  • 1 ≤ n = board[i].length ≤ 12
  • 1 ≤ words.length ≤ 1000
  • 1 ≤ words[i].length ≤ 10
  • board[i][j], words[i]: lowercase English letters
  • the words of words are all different
our 15 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {character[][]} board
 * @param {string[]} words
 * @return {string[]}
 */
function findWords(board, words) {

}
Ctrl ↵
format
reset

saved in this browser

15 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more trie (prefix tree) problems

Pattern “Trie (prefix tree)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.