|
| 1 | +/** |
| 2 | + * @param {character[][]} board |
| 3 | + * @param {string[]} words |
| 4 | + * @return {string[]} |
| 5 | + */ |
| 6 | +var findWords = function (board, words) { |
| 7 | + let foundList = [], |
| 8 | + visited = Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => false)), |
| 9 | + directions = [ |
| 10 | + [1, 0], |
| 11 | + [0, 1], |
| 12 | + [-1, 0], |
| 13 | + [0, -1] |
| 14 | + ] |
| 15 | + |
| 16 | + console.log(board) |
| 17 | + console.log(words) |
| 18 | + |
| 19 | + // dfs |
| 20 | + const exploreNextWord = (word, i, r, c) => { |
| 21 | + console.log(`i:${i},w:${word},r:${r},c:${c}`) |
| 22 | + // console.log("visited") |
| 23 | + // console.log(visited) |
| 24 | + if (word.length === i) return true; |
| 25 | + if ( |
| 26 | + r < 0 || c < 0 || |
| 27 | + r === board.length || c === board[0].length || |
| 28 | + visited[r][c] || |
| 29 | + board[r][c] !== word[i] |
| 30 | + ) return false; |
| 31 | + |
| 32 | + |
| 33 | + visited[r][c] = true; |
| 34 | + i++; |
| 35 | + |
| 36 | + let res = |
| 37 | + exploreNextWord(word, i, r + 1, c) || |
| 38 | + exploreNextWord(word, i, r, c + 1) || |
| 39 | + exploreNextWord(word, i, r - 1, c) || |
| 40 | + exploreNextWord(word, i, r, c - 1); |
| 41 | + |
| 42 | + // console.log(`>>> looking at ${board[r][c]}`) |
| 43 | + // if (word[i] === board[r][c]) { |
| 44 | + // console.log(`>>> comparing with: ${word[i]} === ${board[r][c]} ?`) |
| 45 | + // return true; |
| 46 | + // // char.shift(); |
| 47 | + // // visited = Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => false)); |
| 48 | + // } |
| 49 | + // |
| 50 | + // for (let [dr, dc] of directions) { |
| 51 | + // if (exploreNextWord(char, i, r + dr, r + dc)) return true; |
| 52 | + // } |
| 53 | + |
| 54 | + // Unmark current cell |
| 55 | + visited[r][c] = false; |
| 56 | + |
| 57 | + return res; |
| 58 | + } |
| 59 | + |
| 60 | + // const visited = [...Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => false))] |
| 61 | + // console.log("visited") |
| 62 | + // console.log(visited) |
| 63 | + for (let i = 0; i < words.length; i++) { |
| 64 | + // let tmpVisited = [...visited]; |
| 65 | + for (let r = 0; r < board.length; r++) { |
| 66 | + for (let c = 0; c < board[r].length; c++) { |
| 67 | + if (board[r][c] === words[i][0]) { |
| 68 | + console.log("____________________________________________") |
| 69 | + console.log(`checking:${board[r][c]} === ${words[i][0]}`) |
| 70 | + console.log(words[i]); |
| 71 | + // console.log(words[i].split("").splice(1, words[i].length - 1)) |
| 72 | + // if (exploreNextWord(words[i].split("").splice(1, words[i].length - 1), r, c, tmpVisited)) { |
| 73 | + // if (exploreNextWord(words[i].split("").splice(1, words[i].length - 1), 0, r, c)) { |
| 74 | + if (exploreNextWord(words[i], 0, r, c) && !foundList.includes(words[i])) { |
| 75 | + console.log(" FOUND ASKLLDSAKSDLKJADSLJKASDLKJASDKLJS") |
| 76 | + foundList.push(words[i]); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return foundList; |
| 84 | +}; |
| 85 | + |
| 86 | +/* |
| 87 | +Input: |
| 88 | + board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], |
| 89 | + words = ["oath","pea","eat","rain"] |
| 90 | +Output: ["eat","oath"] |
| 91 | +*/ |
| 92 | + |
| 93 | +let x = |
| 94 | + findWords( |
| 95 | + |
| 96 | + [["o", "a", "b", "n"], ["o", "t", "a", "e"], ["a", "h", "k", "r"], ["a", "f", "l", "v"]], |
| 97 | + ["oa", "oaa"] |
| 98 | + |
| 99 | + // [["a", "b"], ["c", "d"]], |
| 100 | + // ["abcb"] |
| 101 | + |
| 102 | + |
| 103 | + // [["o", "a", "a", "n"], ["e", "t", "a", "e"], ["i", "h", "k", "r"], ["i", "f", "l", "v"]], |
| 104 | + // ["oath", "pea", "eat", "rain"] |
| 105 | + ) |
| 106 | + |
| 107 | +console.log("result") |
| 108 | +console.log(x) |
0 commit comments