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

// Solution1:
// sliding window to get all 10 letters long substring + hashmap
// Time: O(N*L), O(N) for the number of substring, O(L) for the comparation of the substring
// Space: O(N*L)

// Solution2: rabin-karp
// ACGT=>0,1,2,3, calculate the corresponding number of the substring, sliding window to achieve O(L) => O(1) 
// Time: O(N), Space: O(N)

/*
class Solution {
    const int L = 10;
public:
    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> ans;
        unordered_map<string, int> cnt;
        int n = s.length();
        for (int i = 0; i <= n - L; ++i) {
            string sub = s.substr(i, L);
            if (++cnt[sub] == 2) {
                ans.push_back(sub);
            }
        }
        return ans;
    }
};
*/

#if 0
class Solution {
    const int L = 10;
    const int base = 4;
    const int lgBase = 2;
    char bin[26];
public:
    Solution() {
        bin['A'-'A']=0;
        bin['C'-'A']=1;
        bin['G'-'A']=2;
        bin['T'-'A']=3;
    }

    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> ans;
        int n = s.length();
        if (n <= L) {
            return ans;
        }
        int x = 0;
        for (int i = 0; i < L; ++i) {
            x = (x*base) + bin[s[i]-'A'];
        }

        unordered_map<int, int> cnt;
        cnt[x]=1;
        for (int i = L; i < n; ++i) {
            // 2^20 < 2^31
            // [NOTE]: so easy to mix the binary and base operation
            // or value(new_string) = (value(old_string)*base+bin[i])-bin[i-L]*pow(base, L)
            x = (x * base) + bin[s[i]-'A'] - bin[s[i-L]-'A']*pow(base, L);     
            if (++cnt[x] == 2) {
                ans.push_back(s.substr(i-L+1, L));
            }
        }
        return ans;
    }
};
#endif

// optimization：we can use binary operation to accelerate the calculation because the base is 4
class Solution {
    const int L = 10;
    const int lgBase = 2;
    char bin[26];
    const int mask=(1 << (L * lgBase)) - 1;
public:
    Solution() {
        bin['A'-'A']=0;
        bin['C'-'A']=1;
        bin['G'-'A']=2;
        bin['T'-'A']=3;
    }

    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> ans;
        int n = s.length();
        if (n <= L) {
            return ans;
        }
        int x = 0;
        unordered_map<int, int> cnt;
        for (int i = 0; i < L; ++i) {
            x = (x << lgBase) | bin[s[i]-'A'];
        }
        cnt[x]=1;
        for (int i = L; i < n; ++i) {
            // Here, we choose to mask the low 20 bits
            // great idea here !!!
            x = ((x << lgBase) | bin[s[i]-'A']) & mask;
            if (++cnt[x] == 2) {
                ans.push_back(s.substr(i-L+1, L));
            }
        }
        return ans;
    }
};
13209