Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions LeetCode/1559_Detect_Cycles_In_2D_Grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Solution:
def __init__(self):
self.directionX = [-1,0,1,0]
self.directionY = [0,1,0,-1]
def isValid(self, x, y, N, M):
if x < N and x >= 0 and y < M and y >= 0:
return True
return False

def isCycle(self, x, y, arr, visited, parentX, parentY):
# Mark the current vertex as visited
visited[x][y] = True
N, M = len(arr), len(arr[0])

for k in range(4):
newX = x + self.directionX[k]
newY = y + self.directionY[k]
if self.isValid(newX, newY, N, M) and arr[newX][newY] == arr[x][y] and not (parentX == newX and parentY == newY):
if visited[newX][newY]:
return True
else:

check = self.isCycle(newX, newY, arr, visited, x,y)
if check:
return True
return False

def containsCycle(self, grid: List[List[str]]) -> bool:
N, M = len(grid), len(grid[0])
# Initially all the cells are unvisited
visited = [[False] * M for _ in range(N)]

# Variable to store the result
cycle = False

# As there is no fixed position of the cycle
# we have to loop through all the elements
for i in range(N):
if cycle == True:
break

for j in range(M):
## Taking (-1, -1) as source node's parent
if visited[i][j] == False:
cycle = self.isCycle(i, j, grid, visited, -1, -1)

if cycle == True:
break

return cycle