Typical rolling-hash solution. That is, Boyer-Moore algorithm variation.
class Solution { public: inline int encode(char c) { switch (c) { case 'A': return 0; case 'C': return 1; case 'G': return 2; case 'T': return 3; } return 0; } vector<string> findRepeatedDnaSequences(string s) { vector<string> ret; size_t len = s.length(); if (len < 11) return ret; unordered_set<unsigned long> rec; unordered_set<string> rec_s; // init unsigned long hash = 0; for (int i = 0; i < 10; i++) { hash *= 4; hash += encode(s[i]); } rec.insert(hash); // go for (int i = 10; i < len; i++) { hash *= 4; hash += encode(s[i]); hash &= (1 << 20) - 1; if (rec.find(hash) != rec.end()) { string ts = s.substr(i - 9, 10); if (rec_s.find(ts) == rec_s.end()) { rec_s.insert(ts); ret.push_back(ts); } } else { rec.insert(hash); } } return ret; } };