Repeated dna sequences

September 8, 2022

sliding-window

Problem URL: Repeated dna sequences

We will use a sliding window which is 10 characters long. For each window, we will add this to a lookup set. If the window is already present in the lookup set, that means we already have it more than once, we will add this to our result set, we are using set as we don't want duplicate substring. Finally we will return the result once the sliding window reaches the end of the string.

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        lookup, res = set(), set()
        for i in range(len(s)-9):
            sub = s[i:i+10]
            if sub in lookup:
                res.add(sub)
            lookup.add(sub)
        return list(res)

Time Complexity: O(n)
Space Complexity: O(n)