code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

05 Word Search

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const wordSearch = (board, word = '') => {
  const rows = board.length
  const columns = board[0].length

  let visited = new Set()
  const dfs = (pos, i) => {
    const [r, c] = pos
    //console.log(r, c, i)
    visited.add(`${r}${c}`)
  }

  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < columns; j++) {
      dfs([i, j], 0)
    }
  }
  console.log(visited.has(`${2}${3}`))
  return visited
}

console.log(
  wordSearch(
    [
      ['A', 'B', 'C', 'E'],
      ['S', 'F', 'C', 'S'],
      ['A', 'D', 'E', 'E'],
    ],
    // 'ABCCED',
  ),
)

Created 2022-02-24T19:39:05+00:00 · Edit