Difficult:Medium
題目
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 neighboring. The same letter cell may not be used more than once.
翻譯
給定一個 m x n 的字符板網格和一個字符串單詞,如果單詞存在於網格中,則返回 true。
單詞可以由順序相鄰單元格的字母構成,其中相鄰單元格水平或垂直相鄰。同一個字母單元格不能多次使用。
範例
Example 1
1 | Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" |
Example 2
1 | Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE" |
Example 3
1 | Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB" |
解題思路
1.利用遞迴求解
Solution
Code 1 :
1 | const exist = (B, W) => { |