Does the Word Exist in the Grid?
Problem statement
Each string in board is one row of a letter grid. Return whether word can be read along a path of cells.
Start at any cell. Each next cell must share a side with the previous cell, so you may move up, down, left, or right. A cell may appear at most once in the path. The letters along the path must match the complete word in order, including case.
Return only a Boolean. More than one matching path is allowed; finding any one is enough.
Function
wordExists(board: String[], word: String) → booleanExamples
Example 1
board = ["ABCE","SFCS","ADEE"]word = "ABCCED"return = trueThe zero-based cells (0,0), (0,1), (0,2), (1,2), (2,2), (2,1) spell ABCCED. Consecutive cells share a side, and no cell repeats.
Example 2
board = ["ABCE","SFCS","ADEE"]word = "ABCB"return = falseThe grid has only one B cell, while the word needs two B positions. Reusing that cell is forbidden, so no matching path exists.
Constraints
1 <= board.length <= 6.- Every row has the same length, from 1 to 6.
1 <= word.length <= 15.- Board cells and word characters are only ASCII English letters A through Z or a through z.