Graph BFS / DFS/Medium

Number of IslandsPRO PASS

Time: O(M * N)Space: O(M * N)

Step 1: Setup & Initialization - Number of Islands

Flood-fill DFS/BFS to sink land cells.

Array Elements & Pointers
10
[0]
20
[1]
30
[2]
40
[3]
50
[4]
Live Variables & Invariants
status:Initialized
pattern:Number of Islands
Step 1 / 333%
Solution Code
1
function numIslands(grid: string[][]): number {
2
  let count = 0;
3
  for (let r = 0; r < grid.length; r++) {
4
    for (let c = 0; c < grid[0].length; c++) {
5
      if (grid[r][c] === '1') { count++; dfs(grid, r, c); }
6
    }
7
  }
8
  return count;
9
}

Custom Test Case Runner

Input your custom values and visualize step-by-step trace

Quick Presets:

AI DSA Coach

Contextual Tutor for Number of Islands

Hello! I am your AI DSA Tutor for **Number of Islands** (Graph BFS / DFS). Ask me anything about this algorithm, time complexity, or request a step hint!