You are given an m x n grid of lowercase letters and a list of target words. A word is present in the grid if its letters can be spelled out by moving through a sequence of adjacent cells, where adjacent means horizontally or vertically neighboring. The same cell cannot be reused within a single word.
Return every word from the list that can be formed this way. The order of the returned words does not matter, and each found word should appear only once.
Example 1
Input: board = [ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v'] ], words = ['oath','pea','eat','rain']
Output: ['oath','eat']
'oath' traces through connected cells starting at the top-left, and 'eat' can be spelled out; 'pea' and 'rain' have no valid connected path.
Example 2
Input: board = [['a','b'],['c','d']], words = ['abcb']
Output: []
Forming 'abcb' would require revisiting the cell holding 'b', which is not allowed, so no word is returned.
Constraints
m == board.length and n == board[i].length1 ≤ m, n ≤ 121 ≤ words.length ≤ 3 * 10^41 ≤ words[i].length ≤ 10See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI