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

defmodule Solution do
  @spec exist(board :: [[char]], word :: String.t) :: boolean
  def exist(board, word) do
    board =
      board
      |> Enum.map(&List.to_tuple/1)
      |> List.to_tuple()

    w = tuple_size(board) - 1
    h = tuple_size(elem(board, 0)) - 1

    for i <- 0..w, j <- 0..h, reduce: false do
      found -> found || find(board, word, i, j, w, h, [])
    end
  end

  defp find(_board, "", _i, _j, _w, _h, _visited), do: true

  defp find(board, <<char, rest::binary>>, i, j, w, h, visited) do
    cond do
      i < 0 -> false
      i > w -> false
      j < 0 -> false
      j > h -> false
      elem(elem(board, i), j) != char -> false
      {i, j} in visited -> false

      true ->
        visited = [{i, j} | visited]
        find(board, rest, i - 1, j, w, h, visited) ||
        find(board, rest, i + 1, j, w, h, visited) ||
        find(board, rest, i, j - 1, w, h, visited) ||
        find(board, rest, i, j + 1, w, h, visited)
    end
  end
end