# Number of Islands LeetCode Programming Solutions | LeetCode Problem Solutions in C++, Java, & Python [💯Correct]

LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++

Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the Number of Islands in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.

`Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.`

LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.

LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either EasyMedium, or Hard.

LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:

• Mathematics/Basic Logical Based Questions
• Arrays
• Strings
• Hash Table
• Dynamic Programming
• Stack & Queue
• Trees & Graphs
• Greedy Algorithms
• Depth-First Search
• Sorting & Searching
• BST (Binary Search Tree)
• Database
• Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

Link for the ProblemNumber of Islands– LeetCode Problem

`Number of Islands– LeetCode Problem`

### Problem:

Given an `m x n` 2D binary grid `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.

Example 1:

```Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1
```

Example 2:

```Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3
```

Constraints:

• `m == grid.length`
• `n == grid[i].length`
• `1 <= m, n <= 300`
• `grid[i][j]` is `'0'` or `'1'`.
`Number of Islands– LeetCode Solutions`
`Number of Islands Solution in C++:`
```class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
const int m = grid.size();
const int n = grid.size();
const vector<int> dirs{0, 1, 0, -1, 0};
int ans = 0;

auto bfs = [&](int r, int c) {
queue<pair<int, int>> q{{{r, c}}};
grid[r][c] = '2';  // mark '2' as visited
while (!q.empty()) {
const auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
const int x = i + dirs[k];
const int y = j + dirs[k + 1];
if (x < 0 || x == m || y < 0 || y == n)
continue;
if (grid[x][y] != '1')
continue;
q.emplace(x, y);
grid[x][y] = '2';  // mark '2' as visited
}
}
};

for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (grid[i][j] == '1') {
bfs(i, j);
++ans;
}

return ans;
}
};
```
`Number of Islands Solution in Java:`
```class Solution {
public int numIslands(char[][] grid) {
int ans = 0;

for (int i = 0; i < grid.length; ++i)
for (int j = 0; j < grid.length; ++j)
if (grid[i][j] == '1') {
dfs(grid, i, j);
++ans;
}

return ans;
}

private void dfs(char[][] grid, int i, int j) {
if (i < 0 || i == grid.length || j < 0 || j == grid.length)
return;
if (grid[i][j] != '1')
return;

grid[i][j] = '2'; // mark '2' as visited
dfs(grid, i + 1, j);
dfs(grid, i - 1, j);
dfs(grid, i, j + 1);
dfs(grid, i, j - 1);
}
}
```
`Number of Islands Solution in Python:`
```class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
m = len(grid)
n = len(grid)
dirs = [0, 1, 0, -1, 0]

def bfs(r, c):
q = deque([(r, c)])
grid[r][c] = '2'  # mark '2' as visited
while q:
i, j = q.popleft()
for k in range(4):
x = i + dirs[k]
y = j + dirs[k + 1]
if x < 0 or x == m or y < 0 or y == n:
continue
if grid[x][y] != '1':
continue
q.append((x, y))
grid[x][y] = '2'  # mark '2' as visited

ans = 0

for i in range(m):
for j in range(n):
if grid[i][j] == '1':
bfs(i, j)
ans += 1

return ans
``` 