LeetCode-in-All

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.

Example 1:

Input: board = [[“A”,”B”,”C”,”E”],[“S”,”F”,”C”,”S”],[“A”,”D”,”E”,”E”]], word = “ABCCED”

Output: true

Example 2:

Input: board = [[“A”,”B”,”C”,”E”],[“S”,”F”,”C”,”S”],[“A”,”D”,”E”,”E”]], word = “SEE”

Output: true

Example 3:

Input: board = [[“A”,”B”,”C”,”E”],[“S”,”F”,”C”,”S”],[“A”,”D”,”E”,”E”]], word = “ABCB”

Output: false

Constraints:

Follow up: Could you use search pruning to make your solution faster with a larger board?

Solution

# @param {Character[][]} board
# @param {String} word
# @return {Boolean}
def exist(board, word)
  @row_size = board.size
  @col_size = board[0].size

  if word.match(/^#{word[0]}+/)[0].size > word.match(/#{word[-1]}+$/)[0].size
    word.reverse!
  end

  (0...board.size).each do |r|
    (0...board[0].size).each do |c|
      return true if traverse(board, word, 0, r, c)
    end
  end
  false
end

def traverse(board, word, idx, i, j)
  return false if (
  (i < 0 ||
      i >= @row_size) ||
      (j < 0 ||
          j >= @col_size) ||
      word[idx] != board[i][j]
  )
  return true if idx == word.size - 1

  board[i][j] = '#'

  res = (traverse(board, word, idx + 1, i, j + 1) ||
      traverse(board, word, idx + 1, i, j - 1) ||
      traverse(board, word, idx + 1, i + 1, j) ||
      traverse(board, word, idx + 1, i - 1, j))

  board[i][j] = word[idx]

  res
end