<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 {
    int base=5;
    int getValue(char c) {
        int value;
        if(c=='A')   value = 1;
        else if (c=='C')     value = 2;
        else if (c=='G')     value = 3;
        else        value = 4;
        return value;
    }
    int calculateHash(string s, int start, int end) {
        int hash = 0;
        for(int i=start; i<=end; i++) {
            int value=getValue(s[i]);
            hash += value * pow(base, end-i);
        }
        return hash;
    }
public:
    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> res;
        int len = s.length();
        int k = 10;
        if(len<k) {
            return res;
        }

        int previousHash = calculateHash(s, 0, k-1);
        unordered_map<int,int> m;
        m[previousHash] = 1;


        for(int i=k; i<len; i++) {
            int prevCharValue = getValue(s[i-k]);
            int nextCharValue = getValue(s[i]);
            int newHash = (previousHash - (prevCharValue * pow(base,k-1))) * base + nextCharValue;
            if(m.find(newHash)!= m.end()) {
                if(m[newHash]==1) {
                    res.push_back(s.substr(i-k+1, k));
                }
                m[newHash]++;
            } else {
                m[newHash] = 1;
            }
            previousHash = newHash;
        }
    return res;
    }
};
14365