Given a grid of letters and a list of words, identify all words from the list that can be constructed by traversing adjacent cells in the grid. This problem tests your ability to combine graph traversal with efficient data structures for searching.
You are given a two-dimensional array (matrix) of characters representing a letter grid and a list of strings representing a dictionary of words. Your task is to find all the words from the dictionary that can be formed by connecting adjacent letters in the grid. 'Adjacent' means horizontally or vertically neighboring cells. Each cell can only be used once in the formation of a single word.
The brute force approach would involve iterating through each word in the word list and, for each word, scanning the entire board to see if the first letter exists. If it does, we'd perform a depth-first search (DFS) to see if the rest of the word can be formed. Imagine you have a list of errands (words) and you walk through every possible street (board) trying to find each errand location. This is inefficient because you're repeating the search process for each errand. Time complexity is very high, and the space complexity is relatively low since we mostly use existing data structures.
Work through this problem with AI coaching and get real-time feedback
Practice This Problem