leetcode-187-重复的DNA序列

时间:2022-03-20 21:49:51

题目描述:

leetcode-187-重复的DNA序列

方法一:

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        n = 10
        d = {}
        for i in range(len(s)-9):
            if s[i:i+10] in d:
                d[s[i:i+10]] = True
            else:
                d[s[i:i+10]] = False
        return [i for i in d if d[i]]

方法二:

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        n = 10
        d = collections.defaultdict(int)
        for i in range(len(s)-9):
            d[s[i:i+10]] += 1
        return [i for i in d if d[i]>1]

方法三:

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        n=len(s)-9 
        d=set() 
        ans=set() 
        for i in range(n): 
            if s[i:i+10] not in d: 
                d.add(s[i:i+10]) 
            elif s[i:i+10] not in ans: 
                ans.add(s[i:i+10]) 
        return list(ans)