FastPrepStraight-Line Word Search

Straight-Line Word Search

Reddit logoReddit● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a rectangular grid of characters and a nonempty word, return true when the word appears in one straight line.

You may start at any cell and choose one of the eight horizontal, vertical, or diagonal directions. After choosing the direction, every next character must be in the next cell along that same direction. The direction cannot change and the grid does not wrap.

Function

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

Examples

Example 1

board = ["CAT","XXX","DOG"]word = "CAT"return = true

CAT appears left to right in the first row.

Example 2

board = ["AXX","XBX","XXC"]word = "ABC"return = true

ABC appears on the main diagonal.

Example 3

board = ["AB","CD"]word = "ACB"return = false

The letters require a direction change, which is forbidden.

Constraints

  • 1 <= board.length, board[i].length <= 200.
  • All rows have equal length.
  • 1 <= word.length <= 200.
  • The grid and word contain case-sensitive English letters.

More Reddit problems

See Reddit hiring insights
public boolean containsStraightWord(String[] board, String word) {
    // Write your solution here.
}
board["CAT","XXX","DOG"]
word"CAT"
expectedtrue
Checking account…