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"].
class Solution:
# @param s, a string
# @return a list of strings
def findRepeatedDnaSequences(self, s):
rs = []
stringList = {}
l = len(s)
if(l <= 10):
return rs
k = l - 10
while (k >= 0):
if s[k:k+10] not in stringList:
# stringList.append(s[k:k+10])
stringList[s[k:k+10]] = 1
elif s[k:k+10] not in rs:
rs.append(s[k:k+10])
k = k - 1
return rs
My Lesson:
At the beginning, I tried to use stringList as an array.
# stringList.append(s[k:k+10])
However, it will give me an error message: Time exceed limitation.
So I change array into a dictionary, which is based on HashMap. Then it will not give that error.
stringList[s[k:k+10]] = 1