LeetCode 187. Repeated DNA Sequences 20170706 第三十次作業
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.
For example,
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", Return: ["AAAAACCCCC", "CCCCCAAAAA"].
題目大意:給一個DNA字符串,找出所有重復出現的長度為10的子串
解題思路:考慮到子串重復的次數未知,其實可以建立一個字典,該字典的鍵就是該字符串所有可能的10個字符的子串,遍歷該字典的所有鍵,如果該鍵在字典中尚未出現過,則在字典中添加該鍵,如果已經出現過,則該鍵的值加1.最後,輸出所有值大於1的鍵就可以了。
class Solution(object):
def findRepeatedDnaSequences(self, s):
"""
:type s: str
:rtype: List[str]
"""
dict={}
A=[]
for i in range(len(s)-9):
key=s[i:i+10]
if key not in dict:
dict[key]=1
else:
dict[key]+=1
for key in dict:
if dict[key]>1:
A.append(key)
return A
LeetCode 187. Repeated DNA Sequences 20170706 第三十次作業