Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 26 additions & 31 deletions src/count-islands/index.js
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 5 additions & 17 deletions src/word-search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down