Skip to content

Commit ad607dc

Browse files
authored
Merge pull request #1093 from tamilupk/patch-1
Update to prevent TLE issue of 79-Word-Search.py in the Leetcode
2 parents ee85a36 + 7c490cb commit ad607dc

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

python/79-Word-Search.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def dfs(r, c, i):
2424
path.remove((r, c))
2525
return res
2626

27+
# To prevent TLE,reverse the word if frequency of the first letter is more than the last letter's
28+
count = defaultdict(int, sum(map(Counter, board), Counter()))
29+
if count[word[0]] > count[word[-1]]:
30+
word = word[::-1]
31+
2732
for r in range(ROWS):
2833
for c in range(COLS):
2934
if dfs(r, c, 0):

0 commit comments

Comments
 (0)