Menu
CoddyTech

Word Search

You get a grid of letters board, a list of strings where board[r][c] is the letter in row r, column c, and a string word.

Return true if you can trace word: start on any cell and step each time to the cell above, below, left or right, so the cells you visit spell word in order, never using a cell twice. Otherwise return false. Letters are case sensitive.

Function

exist(board: string-array, word: string) → boolean
boardstring-array
the grid, one string of letters per row
wordstring
the word to trace
Returnsboolean
whether word can be traced through side-by-side cells, each used at most once

Constraints

  • 1 ≤ board.length ≤ 6
  • 1 ≤ board[i].length ≤ 6, and every row has the same length.
  • 1 ≤ word.length ≤ 20
  • board and word contain only English letters, uppercase and lowercase.

Examples

Input
board = ["STAR", "POOL", "ENDS"]word = "STOOLS"
Output
true
Explanation
Start on the top-left S, go right to T, down to O, right to O and L, and down to the S below the L. No cell repeats.
challenge icon

Follow-up

Instead of yes or no, can you count how many different tracings of word the board holds?

Reset code
def exist(board, word):
    # Write code here

Test #1

Test #2

Test #3

Arguments

board = ["STAR", "POOL", "ENDS"]
word = "STOOLS"

Expected Output

true