Search⌘ K
AI Features

Word Search II

Explore how to apply the Trie data structure to efficiently locate words from a given list within a 2D grid of letters. Learn to navigate adjacent letter cells and implement a solution that returns all found words, enhancing your skills with patterns commonly tested in coding interviews.

Statement

You are given a list of strings that you need to find in a 2D grid of letters such that the string can be constructed from letters in sequentially adjacent cells. The cells are considered sequentially adjacent when they are neighbors to each other either horizontally or vertically. The solution should return a list containing the strings from the input list that were found in the grid.

Constraints:

  • 11 \leq rows, columns 12\leq 12
  • 11 \leq words.length 3×103\leq 3 \times {10^3}
  • 11 \leq words[i].length 10\leq 10
  • grid[i][j] is an uppercase English letter.
  • words[i] consists of uppercase English letters.
  • All the strings are unique.

Note: The order of the strings in the output does not matter.

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Word Search II

1.

From the given list of words, which words are present in the grid?

OAACETAEIHKSIZLD\begin{matrix} O & A & A & C\\ E & T & A & E\\ I & H & K & S\\ I & Z & L & D \end{matrix}

[OATH,PEA,EAT][OATH, PEA, EAT]

A.

[EAT, OATH]

B.

[EAT, PEA]

C.

[OATH, PEA]

D.

[PEA]


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in main.js in the following coding playground.

JavaScript
usercode > main.js
import Trie from "./trie.js";
import TrieNode from "./trie_node.js";
function findStrings(grid, words){
// Replace this placeholder return statement with your code
return []
}
export {
findStrings
}
Word Search II