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


size_t encode(char letter) {
  switch (letter) {
  case 'A':
    return 0x0;
  case 'C':
    return 0x1;
  case 'G':
    return 0x2;
  case 'T':
    return 0x3;
  }
  return 0;
}

// TODO should maybe be string view
void find_and_insert(size_t num, size_t index_start, const std::string &s,
                     std::unordered_map<size_t, int> &map,
                     std::vector<std::string> &out) {
  auto found = map.find(num);
  if (found == std::end(map)) {
    auto emplace_out = map.emplace(num, 0);
    found = emplace_out.first;
  }
  found->second++;
  if (found->second == 2) {
    out.push_back(s.substr(index_start, 10));
  }
}

class Solution {
public:
  std::vector<std::string> findRepeatedDnaSequences(std::string s) {
    std::unordered_map<size_t, int> set{};
    std::vector<std::string> out{};

    size_t end_len = s.size() >= 10 ? s.size() - 9 : 0;
    size_t prev = 0;
    if (end_len == 0) return out;
    for (size_t j = 0; j < 10; ++j) {

      prev |= encode(s[j]) << (j * 2);
    }
    find_and_insert(prev, 0, s, set, out);
    for (size_t i = 1; i < end_len; ++i) {
      prev = prev >> 2;

      size_t index_start = i;
      // encoding scheme A = 0x0 C = 0x1 G = 0x2 T = 0x3
      size_t num = prev | encode(s[index_start + 9]) << 18;
      prev = num;
      find_and_insert(num, index_start, s, set, out);
      // std::cout << num << std::endl;
    }
    return out;
    // iterate through string (10 letter slices; figure out math)
    // Convert into a number representation
    // throw into unordered map if not already in map. Also append to vector
    // Return a vector which has all the elements of the map
  }
};

19453