码迷,mamicode.com
首页 > 编程语言 > 详细

187. Repeated DNA Sequences Leetcode Python

时间:2015-03-16 13:00:34      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:python   leetcode   

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


187. Repeated DNA Sequences Leetcode Python

标签:python   leetcode   

原文地址:http://blog.csdn.net/hyperbolechi/article/details/44302991

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!