Word Search II

You are given an m x n grid board of lowercase letters and a list of distinct strings words. Return every word from the list that can be traced on the board.

A word is traced by a path of cells that spells it letter by letter, where each cell on the path shares a side (up, down, left or right) with the previous one. Diagonal steps are not allowed, and a single cell can be used at most once within the same word.

Return the found words in any order, each one once.

Constraints

  • 1 <= m, n <= 12
  • 1 <= words.length <= 3 * 10^4
  • 1 <= words[i].length <= 10
  • board and words contain lowercase English letters only, and all the words are distinct.

Examples

Example 1

Input: board = [["c","a","t","s"],["o","r","e","d"],["d","e","n","t"]], words = ["cat","cats","core","code","red","tend","dent","rot","card"]
Output: ["cat","cats","core","code","red","dent"]

Each found word is a chain of side-by-side cells, such as c → o → d → e for "code". "tend" and "card" would need a diagonal last step, and "rot" fails because the only o has no t next to it.

Example 2

Input: board = [["a","b"],["c","d"]], words = ["abdc","abcd","acdb","ba"]
Output: ["abdc","acdb","ba"]

"abcd" would need c right after b, but those two cells only touch diagonally.

Example 3

Input: board = [["a"]], words = ["a","aa"]
Output: ["a"]

"aa" would need the single cell twice: a cell can be used only once per word.

JavaScript you'll use: Recursion, the call stack and memoization Map and Set Graphs and grids in JavaScript

/**
 * @param {string[][]} board
 * @param {string[]} words
 * @return {string[]}
 */
function findWords(board, words) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.