# Help in this Python solution Any reason why my code fails the test case? Help Appreciated

• class Solution:

``````def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
if len(grid) == 0 or grid is None:
return 0
length = len(grid)
width = len(grid[0])
seen = {}
num_groups = 0
for i in xrange(0, length):
for j in xrange(0, width):
if ((i,j) not in seen and grid[i][j] == "1"):
num_groups += 1
self.floodFill(i, j, length, width, seen, grid)
return num_groups

def floodFill(self, i, j, length, width, seen, grid):
if i < 0 or j < 0 or i >= length or j >= width or (i, j) in seen or grid[i][j] != "1":
return
seen[(i,j)] = True
self.floodFill(i+1, j, length, width, seen, grid)
self.floodFill(i, j+1, length, width, seen, grid)
self.floodFill(i-1, j, length, width, seen, grid)
self.floodFill(i, j-1, length, width, seen, grid)
self.floodFill(i+1, j-1, length, width, seen, grid)
self.floodFill(i+1, j+1, length, width, seen, grid)
self.floodFill(i-1, j+ 1, length, width, seen, grid)
self.floodFill(i-1, j-1, length, width,seen, grid)``````

• So in the problem it states "An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically." So this means that you only have to check the top, left, right, and bottom. In your code you seem to checking the diagonals, which there is no need to do. Hope that helps :)

• Thanks. Appreciate that.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.