Simple JS Solution | Number of Islands | Time complexity O( m * n )

Keep traversing through the grid ( row and column) ,
Increment the count ( indicating the no. of Islands) to 1 ,
reseting the number of 1's in the grid to 0 and
keep checking the left , right, top and bottom of the grid[ row][col] whenever 1 is encountered.

Return whenever the grid value is 0 or out of range . -_- simple !

/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function(grid) {
    let count = 0;
    let totalRows = grid.length;
    let totalCols = grid[0].length;
    if (!totalRows) return 0;
    for (let r =0 ; r<totalRows ;r++){
        for( let c =0; c< totalCols ; c++){
            if(grid[r][c] === '1'){
                count++;
                dfs(grid , r ,c);
            }
        }
    }
    return count;
    
    function dfs(grid, r ,c){
        if(r<0 || r >=totalRows || c <0 || c >=totalCols || grid[r][c] === '0') {
            return;
        }
        
        grid[r][c]= '0';
        
        dfs(grid, r,  c-1);
        dfs(grid, r,  c+1);
        dfs(grid, r-1,c);
        dfs(grid, r+1,c);
    }  
};
Comments (0)