Find Number of Islands - The Coding Shala
Home >> Interview Questions >> Find number of Islands Find Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count 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. Example 1: Input: 11110 11010 11000 00000 Output: 1 Example 2: Input: 11000 11000 00100 00011 Output: 3 Number of Islands Java Program Method 1: Using dfs. An Island is started if there is 1 present and surrounded by water(0). we will count island if 1 available and make all the adjacent to zero(0) or sink adjacent island so we don't count them again. We can use dfs or bfs to check adjacent lands. The following code is using dfs. Java Code: class Solution { public int numIslands ( char [][] grid ) { //check if...