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

Commitd8f911f

Browse files
Merge pull requestneetcode-gh#298 from niketanmoon/main
Solved TLE issue. Added removeWord function to update the Trie after …
2 parentsb2736dd +093e405 commitd8f911f

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

‎212-Word-Search-II.py‎

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,65 @@ class TrieNode:
22
def__init__(self):
33
self.children= {}
44
self.isWord=False
5-
self.refs=0
5+
6+
classTrie:
7+
def__init__(self):
8+
self.root=TrieNode()
69

710
defaddWord(self,word):
8-
cur=self
9-
cur.refs+=1
11+
current=self.root
1012
forcinword:
11-
ifcnotincur.children:
12-
cur.children[c]=TrieNode()
13-
cur=cur.children[c]
14-
cur.refs+=1
15-
cur.isWord=True
13+
ifcnotincurrent.children:
14+
current.children[c]=TrieNode()
15+
current=current.children[c]
16+
current.isWord=True
1617

1718
defremoveWord(self,word):
18-
cur=self
19-
cur.refs-=1
20-
forcinword:
21-
ifcincur.children:
22-
cur=cur.children[c]
23-
cur.refs-=1
19+
node=self.root
20+
path= [(None,node)]
21+
forcharinword:
22+
node=node.children[char]
23+
path.append((char,node))
24+
node.isWord=False
2425

26+
foriinrange(len(path)-1,0,-1):
27+
char,currNode=path[i][0],path[i][1]
28+
parentNode=path[i-1][1]
29+
ifcurrNode.isWordorcurrNode.children:
30+
break
31+
delparentNode.children[char]
2532

2633
classSolution:
2734
deffindWords(self,board:List[List[str]],words:List[str])->List[str]:
28-
root=TrieNode()
29-
forwinwords:
30-
root.addWord(w)
35+
trie=Trie()
3136

32-
ROWS,COLS=len(board),len(board[0])
33-
res,visit=set(),set()
37+
# Converting words list to a trie
38+
forwordinwords:
39+
trie.addWord(word)
40+
41+
rows,columns=len(board),len(board[0])
42+
result,visit=set(),set()
3443

3544
defdfs(r,c,node,word):
36-
if (
37-
r<0
38-
orc<0
39-
orr==ROWS
40-
orc==COLS
41-
orboard[r][c]notinnode.children
42-
ornode.children[board[r][c]].refs<1
43-
or (r,c)invisit
44-
):
45+
if (r<0orc<0or
46+
r==rowsorc==columnsor
47+
board[r][c]notinnode.childrenor (r,c)invisit):
4548
return
4649

4750
visit.add((r,c))
4851
node=node.children[board[r][c]]
4952
word+=board[r][c]
5053
ifnode.isWord:
51-
node.isWord=False
52-
res.add(word)
53-
root.removeWord(word)
54+
result.add(word)
55+
trie.removeWord(word)
5456

5557
dfs(r+1,c,node,word)
5658
dfs(r-1,c,node,word)
5759
dfs(r,c+1,node,word)
5860
dfs(r,c-1,node,word)
5961
visit.remove((r,c))
6062

61-
forrinrange(ROWS):
62-
forcinrange(COLS):
63-
dfs(r,c,root,"")
64-
65-
returnlist(res)
63+
forrinrange(rows):
64+
forcinrange(columns):
65+
dfs(r,c,trie.root,"")
66+
returnlist(result)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp