diff --git a/src/count-islands/index.js b/src/count-islands/index.js index ec88f6b..405fed0 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,3 +1,37 @@ -function countIslands(grid) {} +function countIslands(grid) { + const locateIsland = (grid, x, y, checked) => { + if (x < 0 || x > grid.length - 1 || y < 0 || y > grid[x].length - 1) return; + + if (checked[x][y] === true) return; + + checked[x][y] = true; + if (grid[x][y] === 0) return; + + // @traversing the 2D Array + locateIsland(grid, x - 1, y, checked); + locateIsland(grid, x + 1, y, checked); + locateIsland(grid, x, y - 1, checked); + locateIsland(grid, x, y + 1, checked); + }; + + // @creation of boolean 2D Array of equal size with grid + // to help keep track of the visited node + let checked = []; + let count = 0; + for (let i = 0; i < grid.length; i++) { + checked.push([]); + } + + for (let x = 0; x < grid.length; x++) { + for (let y = 0; y < grid[x].length; y++) { + if (!checked[x][y] && grid[x][y] === 1) { + count++; + locateIsland(grid, x, y, checked); + } + checked[x][y] = true; + } + } + return count; +} module.exports = countIslands; diff --git a/src/count-islands/test.js b/src/count-islands/test.js index 4ee76ba..e36a220 100644 --- a/src/count-islands/test.js +++ b/src/count-islands/test.js @@ -25,12 +25,10 @@ describe("Count Islands", () => { test("4x4 island", () => { const grid = [ - [ - [1, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1], - [0, 1, 0, 0], - ], + [1, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1], + [0, 1, 0, 0], ]; expect(countIslands(grid)).toBe(4); diff --git a/src/word-search/index.js b/src/word-search/index.js index 2e385e5..391bcb4 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -1,3 +1,47 @@ -function wordSearch(words, word) {} +function wordSearch(words, word) { + const findLink = (words, x, y, checked, word_index) => { + if (x < 0 || x > words.length - 1 || y < 0 || y > words[x].length - 1) + return; + + if (checked[x][y] === true) return; + + if (words[x][y] !== word[word_index]) return; + + checked[x][y] = true; + + if (word_index === word.length - 1) { + return true; + } + + // @traversing four possible directions (left, right, up, down) + if ( + findLink(words, x - 1, y, checked, word_index + 1) || + findLink(words, x + 1, y, checked, word_index + 1) || + findLink(words, x, y - 1, checked, word_index + 1) || + findLink(words, x, y + 1, checked, word_index + 1) + ) { + return true; + } else { + checked[x][y] = false; + } + }; + + // @boolean 2D Array of equal size with words + // to help keep track of the visited node + let checked = []; + for (let i = 0; i < words.length; i++) { + checked.push([]); + } + + for (let x = 0; x < words.length; x++) { + for (let y = 0; y < words[x].length; y++) { + if (!checked[x][y] && words[x][y] === word[0]) { + if (findLink(words, x, y, checked, 0)) return true; + } + } + } + + return false; +} module.exports = wordSearch; diff --git a/src/word-search/test.js b/src/word-search/test.js index c5498a3..9a83251 100644 --- a/src/word-search/test.js +++ b/src/word-search/test.js @@ -122,4 +122,28 @@ describe("Word Search", () => { expect(wordSearch(words, word)).toBe(true); }); + + test("You've got the word", () => { + const words = [ + ["C", "A", "A"], + ["A", "A", "A"], + ["B", "C", "D"], + ]; + + const word = "AAB"; + + expect(wordSearch(words, word)).toBe(true); + }); + + test("Because you can See", () => { + const words = [ + ["A", "B", "C", "E"], + ["S", "F", "C", "S"], + ["A", "D", "E", "E"], + ]; + + const word = "SEE"; + + expect(wordSearch(words, word)).toBe(true); + }); });