LeetCode 187. Repeated DNA Sequences 20170706 第三十次作业

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 187. Repeated DNA Sequences 20170706 第三十次作业相关的知识,希望对你有一定的参考价值。

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 第三十次作业的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 187 Repeated DNA Sequences

LeetCode 187. Repeated DNA Sequences 20170706 第三十次作业

[LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列

[LeetCode] 187. Repeated DNA Sequences

LeetCode 187. Repeated DNA Sequences(位运算,hash)

187. Repeated DNA Sequences