187. Repeated DNA Sequences(建立词典,遍历一遍 o(n))



All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

Example:

Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

Output: ["AAAAACCCCC", "CCCCCAAAAA"]

 1 class Solution:
 2     def findRepeatedDnaSequences(self, s):
 3         """
 4         :type s: str
 5         :rtype: List[str]
 6         """
 7         d ={}
 8         for i in range(len(s)-9):
 9             if s[i:i+10] not in  d:
10                 d[s[i:i+10]]= 1
11             else:
12                 d[s[i:i+10]]+= 1
13         res =[]
14         for i in d:
15             if d[i]>1:
16                 res.append(i)
17         return res

 

posted @ 2018-06-12 14:02  乐乐章  阅读(136)  评论(0编辑  收藏  举报