Dodomeki
Sign in

97. Word in Grid

Medium

Given a grid of letters and a word, return true if it is possible to trace the letters of the word through adjacent cells horizontally or vertically. The same cell cannot be used more than once in the path. An empty word is considered found.

Example 1
Input: grade = [["M","O","C"],["A","K","A"]], palavra = "MOCK"
Output: true
Example 2
Input: grade = [["A","B"],["C","D"]], palavra = "ABCA"
Output: false
Constraints
  • 1 <= linhas, colunas <= 6
  • 1 <= palavra.length <= 12, ou palavra vazia
  • A grade contains uppercase letters.
BacktrackingMatrixDFS
Caso 1
grade =
[["M","O","C"],["A","K","A"]]
palavra =
"MOCK"
Expected =
false
Caso 2
grade =
[["A","B"],["C","D"]]
palavra =
"ABCA"
Expected =
false

Run checks these 2 cases. Submit runs all 6, including hidden ones.