Word Search
1 approach, code in every language
Problem description
You are given a grid of characters with m rows and n columns along with a target word. Decide whether the word can be traced through the grid by moving between cells that share an edge, that is up, down, left, or right. The path must spell the word in order and may not reuse the same cell more than once.
Return true if such a path exists anywhere in the grid, and false otherwise. Diagonal moves are not allowed.
Example 1
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
Starting at the top-left A, the path A to B to C to C to E to D stays adjacent and reuses no cell.
Example 2
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false
Reaching the final B would require stepping back onto the already-used B cell, which is not allowed.
Constraints
1 <= board.length, board[0].length <= 61 <= word.length <= 15board and word contain only English letters
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Tablet Glyph Trail is our own problem on the technique behind Word Search: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free