dsa · medium

Word Search

board is an m × n grid of letters. Return whether word can be spelled by walking adjacent cells (up/down/left/right, not diagonal). Each cell may be used **at most once** in a walk.

Arguments

Example

board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"true.

word = "SEE"true.

word = "ABCB"false (the first B cannot be reused).

Constraints

1 <= m, n <= 6 1 <= word.length <= 15

Examples

Example 1

Input:
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
"ABCCED"

Expected:
true

Example 2

Input:
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
"SEE"

Expected:
true

Example 3

Input:
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
"ABCB"

Expected:
false

Open in the Dojo editor