PIXELBANKv8.2.1
Menu

Word Search

Given a 2D grid of characters and a string word, return True if the word exists in the grid. The word can be constructed from sequentially adjacent cells (horizontally or vertically). Each cell may only be used once.

Input: grid rows on separate lines, then the word.

Example:

Input:
ABCE
SFCS
ADEE
ABCB
Output:
True
Reasoning:
  • The input grid is searched for the word "ABCCED" (implied, not given in the sample input) in all eight possible directions (up, down, left, right, and four diagonals), but only horizontal and vertical directions are considered in this problem.
  • Starting from each cell, the algorithm checks if the current cell matches the first character of the word, then moves to the adjacent cell (horizontally or vertically) to check the next character.
  • The word "ABCCED" is found in the grid by starting at the cell containing 'A', then moving right to 'B', down to 'C', down to 'C', down to 'E', and down to 'D'.
  • The final output is True because the word is found in the grid.

Constraints:

  • 1 <= m, n <= 6
  • 1 <= len(word) <= 15
  • board and word consist of English letters
Editor

Test Results

0/0
Run code to see test results.