Time: 5329 ms (20.02%), Space: 18.1 MB (21.99%) - LeetHub

This commit is contained in:
Deven
2025-10-02 12:51:32 -04:00
parent f7d8991f8f
commit f504060d03
+35
View File
@@ -0,0 +1,35 @@
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
def existFromStart(x: int, y: int, wordLeft: str, used: Set[Tuple[int, int]]):
if len(wordLeft) == 0:
return True
if board[x][y] != wordLeft[0]:
return False
if len(wordLeft) == 1:
return True
newUsed = used.union(set([(x, y)]))
if x + 1 < len(board) and (x+1,y) not in used and existFromStart(x + 1, y, wordLeft[1:], newUsed):
return True
if x - 1 >= 0 and (x-1,y) not in used and existFromStart(x - 1, y, wordLeft[1:], newUsed):
return True
if y + 1 < len(board[x]) and (x,y+1) not in used and existFromStart(x, y + 1, wordLeft[1:], newUsed):
return True
if y - 1 >= 0 and (x,y-1) not in used and existFromStart(x, y - 1, wordLeft[1:], newUsed):
return True
return False
for i in range(len(board)):
for j in range(len(board[i])):
if existFromStart(i, j, word, set({})):
return True
return False