<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p>

<ul>
	<li>For example, <code>&quot;ACGAATTCCG&quot;</code> is a <strong>DNA sequence</strong>.</li>
</ul>

<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>

<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s[i]</code> is either <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, or <code>&#39;T&#39;</code>.</li>
</ul>

#!/usr/bin/python3

import random

from collections import Counter

class WindowHash:
    def __init__(self, window, modulus = 0, element = 0):
        self.window = window
        self.count = 0

        if modulus == 0:
            modulus = (2**61) - 1
        self.modulus = modulus

        if element == 0:
            element = random.randint(2, modulus)
        self.element = element

        self.shift_factor = pow(element, window - 1, modulus)

        self.hash = 0
        
    def Slide(self, outchar, incoming):
        self.count += 1
        if self.count > self.window:
            self.hash = (self.hash -
                         (ord(outchar) * self.shift_factor)) % self.modulus

        self.hash = (self.hash * self.element
                     + ord(incoming)) % self.modulus

        return self.hash

    def Initialize(self, prefix):
        for ch in prefix:
            self.Slide(None, ch)

        return self.hash
        
class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        if len(s) < 11:
            return []

        window = 10
        W = WindowHash(10)

        N = len(s)
        history = Counter()

        history[ W.Initialize(s[:10]) ] = 1

        dups = []

        for i in range(10, N):
            H = W.Slide(s[i - 10], s[i])
            if history[H] == 1:
                dups.append(s[i - 9:i + 1])
            history[H] += 1
        
        return dups

29200