187. Repeated DNA Sequences Leetcode Python

来源:互联网 发布:电脑监控软件排行 编辑:程序博客网 时间:2024/06/06 02:14

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"].
this Problem can be solve with O(n) time and O(n) space with hashing
class Solution:    # @param s, a string    # @return a list of strings    def findRepeatedDnaSequences(self, s):        dict={}        for i in range(len(s)-9):            key=s[i:i+10]            if key not in dict:                dict[key]=1            else:                dict[key]+=1        res=[]        for elem in dict:            if dict[elem]>1:                res.append(elem)        return res


0 0