Skip to content

Commit

Permalink
impl repeated dna seq
Browse files Browse the repository at this point in the history
  • Loading branch information
SKTT1Ryze committed Mar 14, 2024
1 parent 011d451 commit 964de8f
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions leetcode-cc/RepeatedDNASeq.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <unordered_set>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"
Expand All @@ -10,7 +12,7 @@ IMPLEMENT_PROBLEM_CLASS(
"Given a string s that represents a DNA sequence, return all the "
"10-letter-long sequences (substrings) that occur more than once in a DNA "
"molecule. You may return the answer in any order.",
{""});
{"Hash Table & Sliding Window"});

class SRepeatedDNASeq : public ISolution {
public:
Expand All @@ -22,11 +24,29 @@ class SRepeatedDNASeq : public ISolution {
int test() const override {
return testHelper<string, vector<string>>(
{"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", "AAAAAAAAAAAAA"},
{{"AAAAACCCCC", "CCCCCAAAAA"}, {"AAAAAAAAAA"}},
{{"CCCCCAAAAA", "AAAAACCCCC"}, {"AAAAAAAAAA"}},
[this](auto input) { return this->findRepeatedDnaSequences(input); });
};
int benchmark() const override { return 0; }

private:
vector<string> findRepeatedDnaSequences(string s) const {}
vector<string> findRepeatedDnaSequences(string s) const {
const int N = 10;
int n = s.size();
if (n < N) return {};

unordered_set<string> res = {};
unordered_set<string> memo = {};

for (int i = 0; i <= n - N; i++) {
auto subs = s.substr(i, N);
if (memo.contains(subs)) {
res.insert(subs);
} else {
memo.insert(subs);
}
}

return vector(res.begin(), res.end());
}
};

0 comments on commit 964de8f

Please sign in to comment.