Skip to content

Commit d58d3a9

Browse files
authored
Update repeated-dna-sequences.py
1 parent 0b42c9d commit d58d3a9

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

Python/repeated-dna-sequences.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Time: O(n)
22
# Space: O(n)
3-
#
3+
44
# All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T,
55
# for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
66
#
@@ -12,17 +12,19 @@
1212
#
1313
# Return:
1414
# ["AAAAACCCCC", "CCCCCAAAAA"].
15-
import collections
1615

16+
import collections
1717

18-
class Solution:
19-
# @param s, a string
20-
# @return a list of strings
18+
class Solution(object):
2119
def findRepeatedDnaSequences(self, s):
20+
"""
21+
:type s: str
22+
:rtype: List[str]
23+
"""
2224
dict, rolling_hash, res = {}, 0, []
2325

2426
for i in xrange(len(s)):
25-
rolling_hash = rolling_hash << 3 & 0x3fffffff | ord(s[i]) & 7
27+
rolling_hash = ((rolling_hash << 3) & 0x3fffffff) | (ord(s[i]) & 7)
2628
if rolling_hash not in dict:
2729
dict[rolling_hash] = True
2830
elif dict[rolling_hash]:
@@ -41,6 +43,7 @@ def findRepeatedDnaSequences2(self, s):
4143
l.extend([s[i:i + 10]])
4244
return [k for k, v in collections.Counter(l).items() if v > 1]
4345

46+
4447
if __name__ == "__main__":
4548
print Solution().findRepeatedDnaSequences("AAAAAAAAAA")
4649
print Solution().findRepeatedDnaSequences("")

0 commit comments

Comments
 (0)