diff --git a/src/count-islands/index.js b/src/count-islands/index.js index ec88f6b..ce94a3c 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,3 +1,50 @@ -function countIslands(grid) {} +/* +- use recursion method (method that calls itself) +- use depth first search -module.exports = countIslands; +- create a variable to hold the count +- figure out the edge cases + - if there are no islands then return 0; +- the first loop i will be used to keep track of the outer loop i.e. the rows +- the second loop j will be used to keep track of the inner loop i.e. the columns +- if the grid at any point has 1 it should increase the count; + +- figure out how to keep track of the island + - find an island we change it to 0 to mark as done? i.e. modifying +*/ + +function countIslands(grid) { + let islandCount = 0; + let rows = grid.length + let columns = grid[0].length + + if (rows === 0) { + return 0; + } + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + if (grid[i][j] === 1) { + checkIsland(i, j, grid) + islandCount++; + console.log(islandCount); + + } + } + } + return islandCount; + + function checkIsland(i, j, grid) { + if ((i < 0 || j < 0) || (i >= rows || j >= columns) || grid[i][j] !== 1) { + return; + } + grid[i][j] = 0; + + checkIsland(i + 1, j, grid) + checkIsland(i - 1, j, grid) + checkIsland(i, j - 1, grid) + checkIsland(i, j + 1, grid) + } +}; + +module.exports = countIslands; \ No newline at end of file diff --git a/src/count-islands/test.js b/src/count-islands/test.js index 4ee76ba..2e94806 100644 --- a/src/count-islands/test.js +++ b/src/count-islands/test.js @@ -1,138 +1,142 @@ const countIslands = require("."); describe("Count Islands", () => { - test("flat island", () => { - const grid = [[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1]]; - - expect(countIslands(grid)).toBe(3); - }); - - test("no land in flat sight", () => { - const grid = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]; - - expect(countIslands(grid)).toBe(0); - }); - - test("3x3 island", () => { - const grid = [ - [1, 1, 0], - [0, 0, 1], - [1, 0, 1], - ]; - - expect(countIslands(grid)).toBe(3); - }); - - test("4x4 island", () => { - const grid = [ - [ - [1, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1], - [0, 1, 0, 0], - ], - ]; - - expect(countIslands(grid)).toBe(4); - }); - - test("5x5 island", () => { - const grid = [ - [1, 1, 0, 0, 0], - [0, 1, 0, 0, 1], - [1, 0, 0, 1, 1], - [0, 0, 0, 0, 0], - [1, 0, 1, 0, 1], - ]; - - expect(countIslands(grid)).toBe(6); - }); - - test("Example island - 1", () => { - const grid = [ - [1, 1, 0, 0, 0], - [1, 1, 0, 0, 0], - [0, 0, 1, 0, 0], - [0, 0, 0, 1, 1], - ]; - - expect(countIslands(grid)).toBe(3); - }); - - test("Example island - 2", () => { - const grid = [ - [1, 1, 1, 1, 0], - [1, 1, 0, 1, 0], - [1, 1, 0, 0, 0], - [0, 0, 0, 0, 0], - ]; - - expect(countIslands(grid)).toBe(1); - }); - - test("large island - 1", () => { - const grid = [ - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [1, 0, 0, 0, 1], - [0, 0, 0, 0, 0], - [0, 1, 0, 1, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 1, 0, 0], - [0, 0, 0, 0, 0], - [1, 0, 0, 0, 1], - [0, 0, 0, 0, 0], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - ]; - - expect(countIslands(grid)).toBe(9); - }); - - test("all lands - 5x18", () => { - const grid = [ - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - ]; - - expect(countIslands(grid)).toBe(1); - }); - - test("large lands - 20x9", () => { - const grid = [ - [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1], - [1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], - [1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], - [1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], - [1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], - [1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1], - [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0], - [1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], - [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], - ]; - - expect(countIslands(grid)).toBe(10); - }); -}); + test("flat island", () => { + const grid = [ + [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1] + ]; + + expect(countIslands(grid)).toBe(3); + }); + + test("no land in flat sight", () => { + const grid = [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + ]; + + expect(countIslands(grid)).toBe(0); + }); + + test("3x3 island", () => { + const grid = [ + [1, 1, 0], + [0, 0, 1], + [1, 0, 1], + ]; + + expect(countIslands(grid)).toBe(3); + }); + + test("4x4 island", () => { + const grid = [ + + [1, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1], + [0, 1, 0, 0], + + ]; + + expect(countIslands(grid)).toBe(4); + }); + + test("5x5 island", () => { + const grid = [ + [1, 1, 0, 0, 0], + [0, 1, 0, 0, 1], + [1, 0, 0, 1, 1], + [0, 0, 0, 0, 0], + [1, 0, 1, 0, 1], + ]; + + expect(countIslands(grid)).toBe(6); + }); + + test("Example island - 1", () => { + const grid = [ + [1, 1, 0, 0, 0], + [1, 1, 0, 0, 0], + [0, 0, 1, 0, 0], + [0, 0, 0, 1, 1], + ]; + + expect(countIslands(grid)).toBe(3); + }); + + test("Example island - 2", () => { + const grid = [ + [1, 1, 1, 1, 0], + [1, 1, 0, 1, 0], + [1, 1, 0, 0, 0], + [0, 0, 0, 0, 0], + ]; + + expect(countIslands(grid)).toBe(1); + }); + + test("large island - 1", () => { + const grid = [ + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [1, 0, 0, 0, 1], + [0, 0, 0, 0, 0], + [0, 1, 0, 1, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 1, 0, 0], + [0, 0, 0, 0, 0], + [1, 0, 0, 0, 1], + [0, 0, 0, 0, 0], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + ]; + + expect(countIslands(grid)).toBe(9); + }); + + test("all lands - 5x18", () => { + const grid = [ + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + ]; + + expect(countIslands(grid)).toBe(1); + }); + + test("large lands - 20x9", () => { + const grid = [ + [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1], + [1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], + [1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], + [1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0], + [1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], + ]; + + expect(countIslands(grid)).toBe(10); + }); +}); \ No newline at end of file diff --git a/src/word-search/index.js b/src/word-search/index.js index 2e385e5..b458ea1 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -1,3 +1,73 @@ -function wordSearch(words, word) {} +/* +- use recursion method (method that calls itself) +- use depth first search -module.exports = wordSearch; +- the first loop i will be used to keep track of the outer loop i.e. the rows +- the second loop j will be used to keep track of the inner loop i.e. the columns +- if the letters in the words at any point is equal to the word it should return true +- keep track of the word search by marking letters as visited once we've iterated over it + - +*/ + +function wordSearch(words, word) { + let row = words.length; + let column = words[0].length; + let wordLen = word.length; + + for (let i = 0; i < row; i++) { + for (let j = 0; j < column; j++) { + if (words[i][j] === word[0] && wordCheck(i, j, 0)) return true; + } + } + return false; + + function wordCheck(i, j, wordIndex) { + if (wordIndex === wordLen) return true; + if (i >= row || i < 0 || words[i][j] !== word[wordIndex]) return false; + + words[i][j] = "V"; + + if (wordCheck(i - 1, j, wordIndex + 1) || + wordCheck(i + 1, j, wordIndex + 1) || + wordCheck(i, j - 1, wordIndex + 1) || + wordCheck(i, j + 1, wordIndex + 1)) return true; + + words[i][j] = word[wordIndex]; + return false; + } + +}; + + +// words = [ +// ["C", "D", "Y", "C", "X"], +// ["A", "N", "Y", "Z", "X"], +// ["T", "F", "Z", "A", "T"], +// ["M", "D", "B", "U", "T"], +// ]; + +// word = "CAT"; + +// words = [ +// ["D", "S", "A", "N", "C"], +// ["I", "N", "O", "I", "T"], +// ["T", "A", "T", "R", "O"], +// ["M", "F", "O", "U", "T"], +// ]; + +// word = "SANCTIONS"; + +const words = [ + ["G", "A", "T", "J", "M"], + ["R", "G", "R", "G", "N"], + ["A", "M", "A", "K", "E"], + ["N", "M", "A", "E", "U"], + ["D", "L", "R", "R", "Q"], + ["M", "Y", "M", "A", "A"], +]; + +const word = "GRAMMARLY"; +console.log(wordSearch(words, word)); + + +module.exports = wordSearch; \ No newline at end of file