-
Notifications
You must be signed in to change notification settings - Fork 3
/
q0187.py
69 lines (59 loc) · 1.89 KB
/
q0187.py
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
61
62
63
64
65
66
67
68
#!/usr/bin/python3
from typing import List
class Solution:
def findRepeatedDnaSequences3(self, s: str) -> List[str]:
map = {}
result = list()
for i in range(len(s) - 9):
dna = s[i: i+10]
if map[dna] not in map:
map[dna] = 1
else:
map[dna] += 1
if map[dna] == 2:
result.append(dna)
return result
def findRepeatedDnaSequences(self, s: str) -> List[str]:
result = list()
if len(s) < 11:
return result
chars_map = {"A":0, "C":1, "G":2, "T":3}
record_map = {}
factor = 1 << 20
theNumber = 0
for i in range(10):
theNumber = ((theNumber << 2) + chars_map[s[i]]) % factor
record_map[theNumber] = 1
for i in range(10, len(s)):
theNumber = ((theNumber << 2) + chars_map[s[i]]) % factor
if theNumber in record_map:
record_map[theNumber] += 1
if record_map[theNumber] == 2:
result.append(s[i - 9: i + 1])
else:
record_map[theNumber] = 1
return result
def findRepeatedDnaSequences2(self, s: str) -> List[str]:
tie = {}
result = list()
for i in range(len(s) - 9):
# print(i)
level = 0
p = tie
while level < 9:
char = s[i + level]
if char not in p:
p[char] = {}
p = p[char]
level += 1
char = s[i + level]
if char not in p:
p[char] = 1
else:
p[char] += 1
if p[char] == 2:
result.append(s[i:i + 10])
return result
# s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
# result = Solution().findRepeatedDnaSequences(s)
# print(result)