-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path187.py
More file actions
60 lines (48 loc) · 1.34 KB
/
187.py
File metadata and controls
60 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# coding = utf8
"""
前缀树(Trie)
"""
from typing import List
class TrieNode(object):
def __init__(self, value=''):
self.value = value
self.children = {}
self.cnt = 0
def add_path(self, path):
node = self
for char in path:
if char not in node.children:
child = TrieNode(char)
node.children[char] = child
node = node.children[char]
node.cnt += 1
def repeat_path(self):
res = []
prefix = []
def __dfs(node):
prefix.append(node.value)
if node.cnt > 1:
res.append(''.join(prefix))
for child in node.children.values():
__dfs(child)
prefix.pop()
__dfs(self)
return res
class Solution:
def findRepeatedDnaSequences(self, s: str) -> List[str]:
trie = TrieNode()
N = len(s)
for i in range(0, N-9):
path = s[i:i+10]
trie.add_path(path)
return trie.repeat_path()
if __name__ == '__main__':
cases = [
("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", ["AAAAACCCCC","CCCCCAAAAA"]),
("AAAAAAAAAAAAA", ["AAAAAAAAAA"]),
]
sln = Solution()
for case in cases:
s, golden = case
res = sln.findRepeatedDnaSequences(s)
print(golden, res)