Skip to content

Commit

Permalink
Repeated dna seq (#204)
Browse files Browse the repository at this point in the history
* add repeated dna seq

* impl repeated dna seq
  • Loading branch information
SKTT1Ryze authored Mar 14, 2024
1 parent d9f9cf4 commit 772ff65
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
52 changes: 52 additions & 0 deletions leetcode-cc/RepeatedDNASeq.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <unordered_set>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PRepeatedDNASeq, 187, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"Repeated DNA Sequences",
"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:
size_t problemId() const override { return 187; }
string name() const override {
return ("Solution for " + string("Repeated DNA Sequences"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<string, vector<string>>(
{"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", "AAAAAAAAAAAAA"},
{{"CCCCCAAAAA", "AAAAACCCCC"}, {"AAAAAAAAAA"}},
[this](auto input) { return this->findRepeatedDnaSequences(input); });
};
int benchmark() const override { return 0; }

private:
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());
}
};
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#include "../leetcode-cc/RemoveElement.hpp"
#include "../leetcode-cc/RemoveNthNode.hpp"
#include "../leetcode-cc/ReorderList.hpp"
#include "../leetcode-cc/RepeatedDNASeq.hpp"
#include "../leetcode-cc/RestoreIpAddresses.hpp"
#include "../leetcode-cc/ReverseLinkedListII.hpp"
#include "../leetcode-cc/ReverseNodesinKGroup.hpp"
Expand Down Expand Up @@ -757,5 +758,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SBSTIterator>(); });

handle->registerProblem(
[]() -> ArcProblem { return std::make_shared<PRepeatedDNASeq>(); });
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SRepeatedDNASeq>(); });

return 0;
}

0 comments on commit 772ff65

Please sign in to comment.