Skip to content
BASA DSA

Word Search II

HardTries

Return the distinct dictionary words that can be traced in a letter board using orthogonally adjacent cells, without reusing a cell within one word. Return words in any order.

Example 1

0,0c
0,1a
1,0t
1,1p
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
board = [["c","a"],["t","p"]]
words = ["cat","cap","at","ca","cc"]
Output
["cap","ca"]

Explanation

cap follows (0,0)->(0,1)->(1,1), and ca its first two cells; other words need a diagonal or reuse.

Example 2

0,0a
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
board = [["a"]]
words = ["a","aa"]
Output
["a"]

Explanation

One cell spells a but cannot be reused for aa.

Example 3

0,0x
0,1y
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
board = [["x","y"]]
words = ["z"]
Output
[]

Explanation

No board cell contains z, so no word matches.

Constraints

  • 1 <= rows, columns <= 12
  • At most 30000 distinct nonempty lowercase words of length <= 10

Function contractfindWordsArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…