<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) {
        if (s.size() < 10) return {};  // Early return if string is too short
        
        unordered_map<int, int> sequenceCount;  // To store the 20-bit encoded sequences and their counts
        vector<string> result;
        
        int bitmask = 0;
        unordered_map<char, int> charToBit = {{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};  // Encoding: A=00, C=01, G=10, T=11
        
        // First, process the first 9 characters to set up the bitmask
        for (int i = 0; i < 9; ++i) {
            bitmask = (bitmask << 2) | charToBit[s[i]];  // Build the initial bitmask
        }
        
        // Now slide over the string and process each 10-letter sequence
        for (int i = 9; i < s.size(); ++i) {
            bitmask = ((bitmask << 2) | charToBit[s[i]]) & ((1 << 20) - 1);  // Shift left, add new character, and keep the last 20 bits
            
            sequenceCount[bitmask]++;  // Count occurrences of this 10-letter sequence
            
            // If this sequence has occurred exactly twice, add it to the result
            if (sequenceCount[bitmask] == 2) {
                result.push_back(s.substr(i - 9, 10));  // Add the 10-letter substring to the result
            }
        }
        
        return result;
    }
};
13671