标签:bre function time nas sub series alt 字符 type
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 第三十次作业
标签:bre function time nas sub series alt 字符 type
原文地址:http://www.cnblogs.com/fangdai/p/7127606.html