Word Search II

Hard Solved

Description

Given an m × n board of characters and a list of strings words, return all words on the board.

Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

Input format:

  • Line 1: 2D board as JSON array
  • Line 2: Words array as JSON

Example 1

Input:
[
 ["o","a","a","n"],
 ["e","t","a","e"],
 ["i","h","k","r"],
 ["i","f","l","v"]
]
["oath","pea","eat","rain"]

Output:
["oath","eat"]

Example 2

Input:
[
 ["a","b"],
 ["c","d"]
]
["abcb"]

Output:
[]

Note:

Print the result as a JSON array (order does not matter).

No submissions yet.

Discuss Trie optimization, DFS backtracking, and pruning techniques.

Test Cases