File tree Expand file tree Collapse file tree 3 files changed +10
-10
lines changed
implement-trie-prefix-tree Expand file tree Collapse file tree 3 files changed +10
-10
lines changed Original file line number Diff line number Diff line change 12
12
class Solution :
13
13
def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
14
14
strResult = {}
15
- for str in strs :
16
- sortedStr = '' .join (sorted (str ))
15
+ for word in strs :
16
+ sortedStr = '' .join (sorted (word ))
17
17
if sortedStr not in strResult :
18
18
strResult [sortedStr ] = []
19
- strResult [sortedStr ].append (str )
19
+ strResult [sortedStr ].append (word )
20
20
return list (strResult .values ())
Original file line number Diff line number Diff line change 12
12
space complexity : O(1)
13
13
'''
14
14
15
- class TriedNode :
15
+ class TrieNode :
16
16
def __init__ (self ):
17
17
self .children = {}
18
18
self .isEnd = False
19
19
20
20
class Trie :
21
21
def __init__ (self ):
22
- self .root = TriedNode ()
22
+ self .root = TrieNode ()
23
23
24
24
def insert (self , word : str ) -> None :
25
25
node = self .root
26
26
for char in word :
27
27
if char not in node .children :
28
- node .children [char ] = TriedNode ()
28
+ node .children [char ] = TrieNode ()
29
29
node = node .children [char ]
30
30
node .isEnd = True
31
31
Original file line number Diff line number Diff line change 1
1
'''
2
- TriedNode
2
+ TrieNode
3
3
a : alphabet size
4
4
time complexity : O(m)
5
5
space complexity : O(m * a)
8
8
time complexity : O(n^2)
9
9
space complexity : O(n)
10
10
'''
11
- class TriedNode :
11
+ class TrieNode :
12
12
def __init__ (self ):
13
13
self .children = {}
14
14
self .isEnd = False
15
15
class Trie :
16
16
def __init__ (self ):
17
- self .root = TriedNode ()
17
+ self .root = TrieNode ()
18
18
19
19
def insert (self , word ):
20
20
node = self .root
21
21
for char in word :
22
22
if char not in node .children :
23
- node .children [char ] = TriedNode ()
23
+ node .children [char ] = TrieNode ()
24
24
node = node .children [char ]
25
25
node .isEnd = True
26
26
You can’t perform that action at this time.
0 commit comments