FastPrepDoes the Word Exist in the Grid?

Does the Word Exist in the Grid?

Intuit logoIntuit● MediumNEW GRADOA

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) → boolean

Examples

Example 1

board = ["ABCE","SFCS","ADEE"]word = "ABCCED"return = true

The 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 = false

The 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.

More Intuit problems

See Intuit hiring insights
Saved
public boolean wordExists(String[] board, String word) {
    // Write your code here
}
board["ABCE","SFCS","ADEE"]
word"ABCCED"
expectedtrue
Checking account…