Skip to content

Commit 994aca2

Browse files
committed
optimize word-search
1 parent 90628c0 commit 994aca2

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

word-search/parkhojeong.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ def exist(self, board: List[List[str]], word: str) -> bool:
77

88
board_freq = Counter(ch for row in board for ch in row)
99

10+
for ch, need in Counter(ch for ch in word).items():
11+
if board_freq.get(ch, 0) < need:
12+
return False
13+
1014
if board_freq.get(word[0], 0) > board_freq.get(word[-1], 0):
1115
word = word[::-1]
1216

@@ -17,10 +21,6 @@ def in_bounds(r: int, c: int) -> bool:
1721
return 0 <= r < row_len and 0 <= c < col_len
1822

1923
def backtrack(r: int, c: int, idx: int) -> bool:
20-
for ch, need in Counter(word[idx:]).items():
21-
if board_freq.get(ch, 0) < need:
22-
return False
23-
2424
if idx == len(word) - 1:
2525
return True
2626

0 commit comments

Comments
 (0)