Word Search II

Advanced

Welcome to Word Search II! The goal of this challenge is to create a function that can find and return all the hidden words from a grid of letters.

You will be provided with a grid of letters and a list of words. The grid is represented as a list of lists, where each sublist is a row of letters. Your task is to find and return the list of words that can be constructed from sequentially adjacent cells of the grid (horizontally or vertically).

Example

Let's say you have the grid:

g r a t e
w o r l d
p y t h o

And the list of words: ["grow", "rate", "python", "world"]

  • "grow" can be formed by: g -> r -> o -> w
  • "rate" can be formed by: r -> a -> t -> e
  • "world" can be formed by: w -> o -> r -> l -> d
  • "python" can't be formed: the grid has no "n"

So the function should return:

["grow", "rate", "world"]

Function Signature

def word_search(grid: list[list[str]], words: list[str]) -> list[str]:

Your Task

Implement the function word_search that takes in:

  • grid: a list of lists containing letters.
  • words: a list of words to search in the grid.

The function should return a list of words found in the grid.

Enjoy the challenge and happy coding!

Geek
Inputs

Arg 1:

[["g","r","a","t","e"],["w","o","r","l","d"],["p","y","t","h","o"]]

Arg 2:

["grow","rate","python","world"]
Expected Output
["grow", "rate", "world"]
Actual Output