Skip to content
Onsite

Word Search II

HardP3Coding

Given an m x n grid of letters board and a list of words, return every word from the list that can be formed on the board. A word is formed by a path of adjacent cells (up, down, left, right) whose letters spell it in order; a cell may not be used twice within one word. Return each found word once, in any order.

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"]

Example 2

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

Constraints

  • 1 <= m, n <= 12
  • 1 <= words.length <= 3 * 10^4
  • 1 <= words[i].length <= 10
  • Letters are lower-case; words are distinct

Hints

Run checks the 3 visible tests, Submit all 8.