Leetcode 187. Repeated DNA Sequences
LeetCode 187: Repeated DNA Sequences
The problem statement is as follows:
All DNA is composed of a series of nucleotides abbreviated as ‘A’, ‘C’, ‘G’, and ‘T’, for example: “ACGAATTCCG”. When studying DNA, it is generally useful to identify repeated sequences within the DNA.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
Example 1:
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" Output: ["AAAAACCCCC","CCCCCAAAAA"]
Example 2:
Input: s = "AAAAAAAAAAAAA" Output: ["AAAAAAAAAA"]
Assuming the input string will be valid and consist of uppercase characters ‘A’, ‘C’, ‘G’, and ‘T’.
To solve this problem, let’s follow these steps:
#include <vector>
#include <string>
#include <unordered_set>
using namespace std;
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<string> seen, repeated;
vector<string> result;
for (int i = 0; i + 9 < s.size(); ++i) {
string substring = s.substr(i, 10);
if (seen.find(substring) != seen.end()) {
repeated.insert(substring);
} else {
seen.insert(substring);
}
}
for (const string& str : repeated) {
result.push_back(str);
}
return result;
}
};
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?