diff --git a/src/count-islands/index.js b/src/count-islands/index.js index 40b0c7e..8c51421 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,41 +1,36 @@ +//The helping function helps to turn the surrounding of every island to lowland function countIslands(grid) { - - let markedIsland = function (grid, i, j, visited) { - if (i < 0 || i > grid.length - 1 || j < 0 || j > grid[i].length - 1) { - return; + let count = 0; + //Locate each island and pass it position into the helping function + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[0].length; j++) { + if (grid[i][j] === 1) { + count++; + markSpot(grid, i, j); + } } + } + return count; +} - if (visited[i][j] === "#") { - return; - } +//The helping function helps to turn the surrounding of every island to lowland +function markSpot(grid, rowIndex, colIndex) { + //This ensures that the search is withing the grid + if (!grid[rowIndex] || !grid[rowIndex][colIndex]) { + return; + } - visited[i][j] = "#"; - if(grid[i][j] === 0) { - return; - } + if (grid[rowIndex][colIndex] === 0) { //Identify any lowland and then skip it + return; + } - let row = [-1,1,0,0]; - let col = [0,0,-1,1]; - for (let k = 0; k < row.length; k++) { - markedIsland(grid, i + row[k], j + col[k], visited); - } - }; + grid[rowIndex][colIndex] = 0; //Turns the present island to low - let visited = []; - for(let n = 0; n < grid.length; n++) { - visited[n] = []; - } - let noOfIslands = 0; - for (let i = 0; i < grid.length; i++) { - for (let j = 0; j < grid[i].length; j++) { - if (!visited[i][j] && grid[i][j] === 1) { - noOfIslands++; - markedIsland(grid, i, j, visited); - } - visited[i][j] = "#"; - } + let row = [-1,1,0,0]; //Increments of the adjacent rows and columns + let col = [0,0,-1,1]; + for (let i = 0; i < row.length; i++) { + markSpot(grid, rowIndex + row[i], colIndex + col[i]); //Marks adjacent island with this recursion } - return noOfIslands; } module.exports = countIslands; diff --git a/src/word-search/index.js b/src/word-search/index.js index f274322..80a93c5 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -22,27 +22,15 @@ function wordSearch(words, word) { if (words[rowIndex][colIndex] !== word[charIndex]) { return; - } + } if (words[rowIndex][colIndex] !== "#" && words[rowIndex][colIndex] === word[charIndex]) { words[rowIndex][colIndex] = "#"; - if (nextChar(charIndex + 1, rowIndex - 1, colIndex)) { - return true; - } - - if (nextChar(charIndex + 1, rowIndex + 1, colIndex)) { - return true; - } - - if (nextChar(charIndex + 1, rowIndex, colIndex - 1)) { - return true; - } - - if (nextChar(charIndex + 1, rowIndex, colIndex + 1)) { - return true; - } - + if (nextChar(charIndex + 1, rowIndex - 1, colIndex)) {return true;} + if (nextChar(charIndex + 1, rowIndex + 1, colIndex)) {return true;} + if (nextChar(charIndex + 1, rowIndex, colIndex - 1)) {return true;} + if (nextChar(charIndex + 1, rowIndex, colIndex + 1)) {return true;} } words[rowIndex][colIndex] = word[charIndex];