-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.py
40 lines (30 loc) · 1.12 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution:
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
# 55%
if not(board): return
visit= set()
rowN, colN = len(board), len(board[0])
def helper(x, y, pattern):
if x<0 or x>rowN-1 or y<0 or y>colN-1:
return
if (x, y) in visit:
return
if pattern != board[x][y]:
return
visit.add((x, y))
helper(x-1, y, pattern)
helper(x+1, y, pattern)
helper(x, y-1, pattern)
helper(x, y+1, pattern)
for i in range(rowN):
for j in range(colN):
if i==0 or i==rowN-1 or j==0 or j==colN-1:
helper(i, j, board[i][j])
for i in range(1, rowN):
for j in range(1, colN):
if not((i, j) in visit):
board[i][j]="X"