【leetcode】187. Repeated DNA Sequences
题目如下:
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"]
解题思路:题目本身很简单,依次判断每个子串s[i:i+10]是否重复即可。
代码如下:
class Solution(object): def findRepeatedDnaSequences(self, s): """ :type s: str :rtype: List[str] """ dic = {} end = 10 substring = s[:end] dic[substring] = end - 1 res = [] while end < len(s): substring = substring[1:] + s[end] end += 1 if substring in dic: if dic[substring] == -1: continue res.append(substring) dic[substring] = -1 else: dic[substring] = end - 1 return res