<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:
    std::vector<std::string>
    findRepeatedDnaSequences(const std::string_view s) {
        if (s.size() < 10u)
            return {};

        // Store sequence of 10 symbols in single 64-bit number.
        // Each symbol uses 5 bits.

        // Prepare initial string representation.
        uint64_t code =
            (uint64_t(s[0] - 'A') << 00u) | (uint64_t(s[1] - 'A') << 05u) |
            (uint64_t(s[2] - 'A') << 10u) | (uint64_t(s[3] - 'A') << 15u) |
            (uint64_t(s[4] - 'A') << 20u) | (uint64_t(s[5] - 'A') << 25u) |
            (uint64_t(s[6] - 'A') << 30u) | (uint64_t(s[7] - 'A') << 35u) |
            (uint64_t(s[8] - 'A') << 40u) | (uint64_t(s[9] - 'A') << 45u);

        // Collect all codes into vector.
        std::vector<uint64_t> codes;
        codes.reserve(s.size() - 9u);

        // Use sliding window approach.
        for (size_t i = 10u; i < s.size(); ++i) {
            codes.push_back(code);
            code = (code >> 5u) | (uint64_t(s[i] - 'A') << 45u);
        }
        if (s.size() > 10u)
            codes.push_back(code);

        // Sort codes to group identical codes.
        std::sort(codes.begin(), codes.end());

        std::vector<std::string> res;
        // Iterate over sorted codes, search for duplicates.
        auto it = codes.begin();
        while (it != codes.end()) {
            auto next_it = std::next(it);
            if (next_it == codes.end())
                break;
            while (next_it != codes.end() && *next_it == *it)
                ++next_it;

            if (std::next(it) < next_it) {
                // Convert compact representation back into string.
                std::string sequence;
                sequence.resize(10u);
                const uint64_t code = *it;
                for (uint64_t i = 0; i < 10u; ++i)
                    sequence[i] = 'A' + ((code >> (i * 5u)) & 31u);
                res.emplace_back(sequence);
            }

            it = next_it;
        }
        return res;
    }
};
9971