Skip to content
Decoder

Word Search II

Hard

Problem

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.

Example 1

board = [["o", "a", "a", "n"],

Example 2

         ["e", "t", "a", "e"],

Example 3

         ["i", "h", "k", "r"],

Example 4

         ["i", "f", "l", "v"]]

Example 5

Input
words = ["oath", "pea", "eat", "rain"]
Output
["eat", "oath"]

Example 6

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

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.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.