Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
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"]

My code:

class Solution(object):
    def findRepeatedDnaSequences(self, s):
        mydict = {}
        for i in range(len(s)):
            string = s[i:i+10]
            mydict[string] = mydict[string] + 1 if string in mydict else 1
        return [item for item in mydict if mydict[item]>1]
    def findRepeatedDnaSequences1(self, s):
        """
        :type s: str
        :rtype: List[str]
        """
        if len(s)<10:
            return []
        result=[]
        record_dict={}
        for i in range(len(s)-9):
            check_str=s[i:i+10]
            if check_str not in record_dict.keys():
                record_dict[check_str]=1
            else:
                record_dict[check_str]+=1
        #print(record_dict)
        
        return [item for item in record_dict if record_dict[item]>1]
                

Solution: https://leetcode.com/problems/repeated-dna-sequences/