<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:
    vector<string> findRepeatedDnaSequences(string s) {
        unordered_map<int,int>mp; // hash value to count 
        unordered_map<char,int>pair = {{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};

        int hash_val = 0;
        int base = 4;
        int n = s.length();
        if(n < 10) return vector<string>();

        // Initialize the first hash value for the first 10-letter sequence
        int power = pow(4,9);
        for(int i = 0; i < 10; i++){
            int val = pair[s[i]];
            hash_val += (val*power);
            power /= 4;
        }
        
        mp[hash_val] = 1;
        unordered_set<string> ans;
        int highest_power = pow(base, 9); // 4^9
        
        // Rolling hash calculation for the remaining sequences
        for(int i = 1; i < n-10+1; i++){
            // Remove the first character and add the new character
            hash_val = (hash_val - pair[s[i-1]] * highest_power) * base + pair[s[i+9]];
            
            if(mp[hash_val] == 1) {
                ans.insert(s.substr(i, 10));
            }
            mp[hash_val]++;
        }
        
        vector<string> result(ans.begin(), ans.end());
        return result;
    }
};

14134