Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5041074

Browse files
authored
Merge pull requestneetcode-gh#328 from AgranatMarkit/main
Update 212-Word-Search-II.py
2 parents05acc27 +89f538b commit5041074

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

‎212-Word-Search-II.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ class TrieNode:
22
def__init__(self):
33
self.children= {}
44
self.isWord=False
5+
self.refs=0
56

67
defaddWord(self,word):
78
cur=self
9+
cur.refs+=1
810
forcinword:
911
ifcnotincur.children:
1012
cur.children[c]=TrieNode()
1113
cur=cur.children[c]
14+
cur.refs+=1
1215
cur.isWord=True
16+
17+
defremoveWord(self,word):
18+
cur=self
19+
cur.refs-=1
20+
forcinword:
21+
ifcincur.children:
22+
cur=cur.children[c]
23+
cur.refs-=1
1324

1425

1526
classSolution:
@@ -24,14 +35,18 @@ def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
2435
defdfs(r,c,node,word):
2536
if (r<0orc<0or
2637
r==ROWSorc==COLSor
27-
board[r][c]notinnode.childrenor (r,c)invisit):
38+
board[r][c]notinnode.childrenor
39+
node.children[board[r][c]].refs<1or
40+
(r,c)invisit):
2841
return
2942

3043
visit.add((r,c))
3144
node=node.children[board[r][c]]
3245
word+=board[r][c]
3346
ifnode.isWord:
47+
node.isWord=False
3448
res.add(word)
49+
root.removeWord(word)
3550

3651
dfs(r+1,c,node,word)
3752
dfs(r-1,c,node,word)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp