Word Search

Medium

Given an m x n grid of letters board and a string word, return true if word can be found in the grid.

The word has to be spelled out along a path of cells, where each cell is next to the one before it: directly up, down, left or right of it, not diagonal. A cell can't be used more than once in the same word.

Letters are case-sensitive, so "a" and "A" are different letters.

Examples

Example 1

Input:  board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
Example 1: word = "ABCCED"ABCESFCSADEEA → B → C→ C → E → DEach step goes up, down,left or right.

Example 2

Input:  board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output: true

Start at the S at the end of the middle row, go down to the E below it, then left to the next E.

Example 3

Input:  board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false
Example 3: word = "ABCB"ABCESFCSADEEA → B → C → B?The only B next to the Cis already in the word, anda cell can't be used twice.

Example 4

Input:  board = [["a","b"],["c","d"]], word = "abcd"
Output: false

b and c only touch at a corner, and diagonal steps aren't allowed.

Example 5

Input:  board = [["a","b"],["c","d"]], word = "abdc"
Output: true

Go right from a to b, down to d, then left to c.

Constraints

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

Follow-up

Can you skip searches that are sure to fail, so that it still runs fast on a bigger board?

TypeScript
Loading editor…
Test cases35
Case 1
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
word = "ABCCED"
Case 2
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
word = "SEE"
Case 3
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
word = "ABCB"
Case 4
board = [["a"]]
word = "a"
Case 5
board = [["a","b"],["c","d"]]
word = "abcd"
Case 6
board = [["a","b"],["c","d"]]
word = "abdc"
Case 7
board = [["a"]]
word = "b"
Case 8
board = [["a"]]
word = "aa"
Case 9
board = [["a","b"],["c","d"]]
word = "abdca"
Case 10
board = [["a","A"]]
word = "Aa"
Case 11
board = [["a","A"]]
word = "AA"
Case 12
board = [["A","B","C","E"],["S","F","E","S"],["A","D","E","E"]]
word = "ABCESEEEFS"
Case 13
board = [["C","A","A"],["A","A","A"],["B","C","D"]]
word = "AAB"
Case 14
board = [["a","b","c"],["a","e","d"],["a","f","g"]]
word = "abcdefg"
Case 15
board = [["a","b","c"],["a","e","d"],["a","f","g"]]
word = "gfedcbaaa"
Case 16
board = [["a","b","c"],["a","e","d"],["a","f","g"]]
word = "eaabcdgfa"
Case 17
board = [["A","B","C","D","E","F"]]
word = "FEDCBA"
Case 18
board = [["A"],["B"],["C"],["D"],["E"],["F"]]
word = "CDE"
Case 19
board = [["A"],["B"],["C"],["D"],["E"],["F"]]
word = "ACE"
Case 20
board = [["A","A","A","B","B","A"],["A","A","B","A","B","B"],["B","B","A","B","A","A"],["B","B","A","A","A","A"],["B","A","A","A","B","A"],["B","B","B","A","B","B"]]
word = "BAABBAAAABBBAAA"
Case 21
board = [["B","B","A","A","A","B"],["B","A","B","B","B","B"],["A","B","A","A","B","A"],["A","B","B","B","A","A"],["A","A","B","B","B","A"],["A","A","A","B","A","A"]]
word = "AABAABAAAABBBBA"
Case 22
board = [["A","A","C","C","C","A"],["B","B","A","C","B","C"],["C","C","A","B","B","C"],["C","C","C","B","B","B"],["B","C","A","A","C","A"],["A","B","C","A","B","C"]]
word = "BBCBCCCABBCC"
Case 23
board = [["p","e","p","d","c","a"],["i","t","p","b","l","c"],["n","k","p","d","z","u"],["c","e","a","g","v","o"],["c","i","b","q","h","w"]]
word = "dcacuzdpkncciba"
Case 24
board = [["X","z","X","z","Y","X"],["X","X","z","Y","y","X"],["z","z","y","z","Z","Z"],["y","X","X","x","Z","Z"],["z","x","Z","X","X","z"],["Y","Y","Y","x","Y","z"]]
word = "zYzyXXzXXX"
Case 25
board = [["b","a","a","b"],["a","b","a","b"],["b","b","b","b"],["b","a","a","a"]]
word = "bbbbbaaaa"
Case 26
board = [["B","A","a","A","A"],["A","a","A","A","a"],["b","B","B","a","b"],["a","b","B","b","a"],["B","a","B","B","a"],["B","A","a","A","b"]]
word = "babBBBbabaAAAAa"
Case 27
board = [["S","A","S","A","E","E"],["S","E","E","S","S","S"],["S","S","S","A","A","A"]]
word = "ESAAASE"
Case 28
board = [["l","x","O","o","G","K"],["O","T","b","S","S","J"],["X","O","H","r","S","c"],["n","Y","Q","c","f","r"],["C","o","d","D","j","D"],["u","d","V","v","N","V"]]
word = "QdoCnn"
Case 29
board = [["a","b"],["a","b"]]
word = "bbaa"
Case 30
board = [["B","A","B","B","A","A"],["B","A","A","B","B","B"],["A","A","B","A","A","A"],["A","B","A","A","B","A"],["B","A","A","B","B","A"],["B","B","B","B","A","B"]]
word = "A"
Case 31
board = [["A","A","A","A"],["A","A","A","A"],["A","A","A","A"],["A","A","A","A"]]
word = "AAAAAAAAB"
Case 32
board = [["A","A","A"],["A","A","A"],["A","A","A"],["A","A","A"]]
word = "AAAAAAAAAAAAA"
Case 33
board = [["a","a","a","a","a","a"]]
word = "aaaaaaa"
Case 34
board = [["a","a","a","a","a","a"]]
word = "aaaaaa"
Case 35
board = [["A","A"],["A","A"]]
word = "AAAAB"