Word Search
Problem statement
Given a rectangular character grid represented by the strings in board and a nonempty word, return true if the word can be formed by following a path through the grid.
- The path may start at any cell.
- Each next cell must be immediately above, below, left, or right of the previous cell. Diagonal moves are not allowed.
- A cell may be used at most once in the same path.
- The characters along the path must match
wordin order.
Function
wordSearch(board: String[], word: String) → booleanExamples
Example 1
board = ["ABCE","SFCS","ADEE"]word = "ABCCED"return = trueA path starts at the top-left cell, moves right twice, down twice, then left to spell the word.
Example 2
board = ["ABCE","SFCS","ADEE"]word = "SEE"return = trueStart at the S in the rightmost column, move down to E, then left to E.
Example 3
board = ["ABCE","SFCS","ADEE"]word = "ABCB"return = falseSpelling the final B would require using the same top-row B cell twice.
Constraints
1 <= board.length <= 10.- All rows have the same length
c, with1 <= c <= 10andboard.length * c <= 50. - Grid cells and the word contain only uppercase English letters.
1 <= word.length <= 10.