BFS EASY SIMPLE APPROACH WITH EXPLANATION! BEATS 90.3% TIME COMPLEXITY & 93.2% SPACE COMPLEXITY
IntuitionBFS with a slight twistApproachComplexity
Time complexity:
O(m∗n)
Space complexity:
O(k) | k is the number of islands in the gridCode
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
BFS with a slight twist

# Approach
<!-- Describe your approach to solving the problem. -->
        We can solve this problem using BFS:

            1. First, we traverse the grid and assign a group number to each island (connected component of `1`s). 
            Instead of keeping `1`s in the grid, we replace them with their respective group numbers.

            2. We also calculate the size of each island and store it in a hash table (`group_points`), 
            which maps each group number to its corresponding size.

            3. Next, we perform BFS on each `0` in the grid to determine its maximum contribution if changed to `1`.

            4. When checking a 0, we explore its neighboring groups (islands) and sum up their sizes using the hash table
            group_points. To avoid counting the same island multiple times, we ensure that each group is visited at most once 
            per BFS.

            5. Finally, we return the largest possible island size after flipping a `0` to `1`.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(m * n)$$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(k) | k is the number of islands in the grid
# Code
```python3 []
class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        """
        We can solve this problem using BFS:

            1. First, we traverse the grid and assign a group number to each island (connected component of `1`s). 
            Instead of keeping `1`s in the grid, we replace them with their respective group numbers.

            2. We also calculate the size of each island and store it in a hash table (`group_points`), 
            which maps each group number to its corresponding size.

            3. Next, we perform BFS on each `0` in the grid to determine its maximum contribution if changed to `1`.

            4. When checking a 0, we explore its neighboring groups (islands) and sum up their sizes using the hash table
            group_points. To avoid counting the same island multiple times, we ensure that each group is visited at most once 
            per BFS.

            5. Finally, we return the largest possible island size after flipping a `0` to `1`.
        """
        group_points = defaultdict(int)
        group_num = 2
        m = len(grid)
        n = len(grid[0])
        max_points = 0
        moves = [[-1,0],[1,0],[0,-1],[0,1]]
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    que = deque()
                    que.append([i,j])
                    points = 0
                    while que:
                        a,b = que.popleft()
                        if grid[a][b] != 1:
                            continue
                        points += 1
                        grid[a][b] = group_num
                        for x,y in moves:
                            x += a
                            y += b
                            if 0 <= x < m and 0<= y < n:
                                if grid[x][y] == 1:
                                    que.append([x,y])
                    group_points[group_num] = points
                    max_points = max(max_points, points)
                    group_num += 1
        
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 0:
                    points = 1
                    visited = set()
                    for x,y in moves:
                        x += i
                        y += j
                        if 0 <= x < m and 0<= y < n:
                            if grid[x][y] != 0 and grid[x][y] not in visited:
                                visited.add(grid[x][y])
                                points += group_points[grid[x][y]]
                    
                    max_points = max(max_points, points)

        return max_points

                

        
```
['Hash Table', 'Breadth-First Search', 'Python3']