5. Find the Number of Islands
The problem can be found at the following link: Question Link
Problem Description
Given a grid of size n Γ m consisting of 'W'
(Water) and 'L'
(Land), find the number of islands. An island is formed by connecting adjacent lands in any of the 8 directions (horizontally, vertically, or diagonally) and is surrounded by water or the grid boundary.
Examples
Example 1:
Input:
grid[][] = [['L', 'L', 'W', 'W', 'W'],
['W', 'L', 'W', 'W', 'L'],
['L', 'W', 'W', 'L', 'L'],
['W', 'W', 'W', 'W', 'W'],
['L', 'W', 'L', 'L', 'W']]
Output:
4
Explanation: There are 4 distinct islands in the grid.
Example 2:
Input:
grid[][] = [['W', 'L', 'L', 'L', 'W', 'W', 'W'],
['W', 'W', 'L', 'L', 'W', 'L', 'W']]
Output:
2
Explanation: There are 2 islands in the grid.
Constraints:
$(1 \leq n, m \leq 500)$
grid[i][j]
β {'L'
,'W'
}
My Approach
DFS (Recursive Flood Fill β 8 Directions)
We traverse the grid and whenever we find a land cell 'L'
, we start a DFS flood fill marking all connected lands.
Each such initiation counts as one island.
Algorithm Steps:
Traverse every cell in the grid.
If it's land ('L'), call a recursive DFS function to flood fill all connected lands in 8 directions.
Mark each visited land as
'W'
.Count each DFS initiation as a distinct island.
Time and Auxiliary Space Complexity:
Expected Time Complexity: O(n Γ m), as each cell is processed once.
Expected Auxiliary Space Complexity: O(n Γ m) in the worst-case scenario (e.g., when the grid is completely filled with land) due to the recursion stack or BFS/stack storage.
Code (C++)
class Solution{
public:
int countIslands(vector<vector<char>>& g){
int n = g.size(), m = g[0].size(), ans = 0;
function<void(int,int)> f = [&](int i, int j){
if(i < 0 || i >= n || j < 0 || j >= m || g[i][j] == 'W') return;
g[i][j] = 'W';
for(int a = -1; a <= 1; a++)
for(int b = -1; b <= 1; b++)
f(i + a, j + b);
};
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(g[i][j] == 'L') { ans++; f(i, j); }
return ans;
}
};
Code (Java)
class Solution{
public int countIslands(char[][] g){
int n = g.length, m = g[0].length, ans = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(g[i][j]=='L'){ ans++; dfs(g, i, j, n, m); }
return ans;
}
void dfs(char[][] g, int i, int j, int n, int m){
if(i < 0 || j < 0 || i >= n || j >= m || g[i][j]=='W') return;
g[i][j] = 'W';
for(int a = -1; a <= 1; a++)
for(int b = -1; b <= 1; b++)
dfs(g, i + a, j + b, n, m);
}
}
Code (Python)
class Solution:
def numIslands(self, g):
n, m, ans = len(g), len(g[0]), 0
def dfs(i, j):
if i < 0 or j < 0 or i >= n or j >= m or g[i][j]=='W': return
g[i][j] = 'W'
for a in (-1,0,1):
for b in (-1,0,1):
dfs(i+a, j+b)
for i in range(n):
for j in range(m):
if g[i][j]=='L':
ans += 1
dfs(i, j)
return ans
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated