<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>

class Solution {
public:
    long long bin_power_mod(long long base, long long power, long long mod){
        base%=mod;
        
        long long res = 1;

        while(power > 0){
            if(power & 1){
                res = res * base % mod;
            }
            base = base * base % mod;
            power>>=1;
        }

        return res;
    }

    long long modulo_inverse(long long base, long long mod){
        return bin_power_mod(base, mod-2, mod);
    }
    vector<string> findRepeatedDnaSequences(string s) {
        long long n = s.length();
        if(n<10) return {};
        long long mod = 1e9 + 7;

        vector<long long>hash(n);
        map<char, long long>mapping;
        map<long long, long long>cnt;
        mapping['A'] = 1;
        mapping['C'] = 2;
        mapping['G'] = 3;
        mapping['T'] = 4;

        hash[0] = mapping[s[0]];

        for(long long i=1; i<10; i++){ 
            hash[i] = (((hash[i-1] * 5)%mod) + mapping[s[i]])%mod;
        }
        cnt[hash[9]]++;
        vector<string>ans;
        for(long long i=10; i<n; i++){
            hash[i] = ((((hash[i-1] - ((mapping[s[i-10]]*bin_power_mod(5, 9, mod)) % mod) + mod) % mod) * 5 ) % mod + mapping[s[i]]) % mod;
            if(cnt[hash[i]]==1){
                ans.push_back(s.substr(i-9, 10));
            } 
            cnt[hash[i]]++;
        }

        return ans;
    }
};


19684