Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeated dna seq #204

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading