Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Number of Islands problem solution

YASH PAL, 31 July 2024

In this Leetcode Number of Islands problem solution we have Given an m x n 2D binary grid which represents a map of ‘1’s (land) and ‘0’s (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Leetcode Number of Islands problem solution

Problem solution in Python.

def numIslands(self, grid):
        def dfs(x, y):
            if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] != '1':
                return
            grid[x][y] = '0'
            for i, j in [(x - 1, y), (x, y - 1), (x + 1, y), (x, y + 1)]:
                dfs(i, j)
        
        if not grid or not grid[0]:
            return 0
        res = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == '1':
                    dfs(i, j)
                    res += 1
        return res

Problem solution in Java.

class Solution {
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0)
            return 0;
        int r = grid.length;
        int c = grid[0].length;
        int res = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    res++;
                }
            }
        }
        return res;
    }
    
    private void dfs(char[][] grid, int r, int c) {
        int rowNum = grid.length;
        int colNum = grid[0].length;
        if (r < 0 || r >= rowNum || c < 0 || c >= colNum || grid[r][c] == '0') {
            return;
        }
        grid[r][c] = '0';
        dfs(grid, r + 1, c);
        dfs(grid, r - 1, c);
        dfs(grid, r, c + 1);
        dfs(grid, r, c - 1);
    }
}

Problem solution in C++.

class Solution {
public:
    int row, col;
    void dfs(vector<vector<char>> & grid, int y, int x){
        if(y < 0 || x < 0 || y >= row || x >= col || grid[y][x] != '1')
            return ;
        grid[y][x] = '#';
        dfs(grid, y + 1, x);
        dfs(grid, y - 1, x);
        dfs(grid, y, x - 1);
        dfs(grid, y, x + 1);
    }
    int numIslands(vector<vector<char>>& grid) {
        row = grid.size();
        if(!row) return 0;
        col = grid[0].size();
        int count = 0;
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(grid[i][j] == '1'){
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }
};

Problem solution in C.

int label(char** grid,int row,int col,int gridRowSize, int gridColSize );
int numIslands(char** grid, int gridRowSize, int gridColSize) {
    int row,col;
    int newlabel;
    newlabel=0;
    for(row=0;row<=gridRowSize-1;row++)
    {
       for(col=0;col<=gridColSize-1;col++) 
       {
           newlabel=newlabel+label(grid,row,col,gridRowSize,gridColSize);
       }
        
    }
    
    return newlabel;
}

int label(char** grid,int row,int col,int gridRowSize, int gridColSize )
{
    if (*(*(grid+row)+col)=='1')
    {
        *(*(grid+row)+col)='0';
        if (row!=0)
            label(grid,row-1,col,gridRowSize,gridColSize );
        if (row!=gridRowSize-1)    
            label(grid,row+1,col,gridRowSize,gridColSize );
        if (col!=0)
            label(grid,row,col-1,gridRowSize,gridColSize );
        if (col!=gridColSize-1)    
            label(grid,row,col+1,gridRowSize,gridColSize ); 
        
        return 1;
    }
    else    
        return 0;
  
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes