FastPrepWord Search

Word Search

WhatNot logoWhatNot● MediumINTERNPHONE SCREEN
Learn

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 word in order.

Function

wordSearch(board: String[], word: String) → boolean

Examples

Example 1

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

A 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 = true

Start at the S in the rightmost column, move down to E, then left to E.

Example 3

board = ["ABCE","SFCS","ADEE"]word = "ABCB"return = false

Spelling 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, with 1 <= c <= 10 and board.length * c <= 50.
  • Grid cells and the word contain only uppercase English letters.
  • 1 <= word.length <= 10.

More WhatNot problems

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