 All Problems
Word Search
medium
backtracking
arrays
string
amazon
microsoft
facebook

Given an m x n grid of characters board and a string word, return true if word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighbouring. The same cell may not be used more than once.

Example 1:

Input:
3 4
A B C E
S F C S
A D E E
ABCCED
Output: true

Example 2:

Input:
3 4
A B C E
S F C S
A D E E
SEE
Output: true

Constraints:

  • m == board.length, n == board[i].length
  • 1 ≤ m, n ≤ 6
  • 1 ≤ word.length ≤ 15
  • board and word consist of only uppercase and lowercase English letters.

Input format: First line: m n. Next m lines: n space-separated characters. Last line: the word.

Output format: true or false.

Run to check your code against the sample cases, or submit to run every case