All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes 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.
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", Return: ["AAAAACCCCC", "CCCCCAAAAA"].
思路:我们需要寻找的是重复出现过的子串,看到题目下方的提示 tags 说使用哈希表和位运算,因此我们想到使用数字来表示A,C,G,T,分别将他们标为0,1,2,3,我们可以用总共20位二进制数来表示长度为十的子串,并且用一个int来表示这个二进制数,这样只要两个子串有相同的int,我们就将其放在vector中作为结果输出,由于题目要求输出的子串中没有重复,所以我们使用map来排除重复的情况。
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
map<string, string>result;
if (s.length() <= 10) {
return vector<string>();
}
int tran[s.length()];
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'A') {
tran[i] = 0;
}
if (s[i] == 'C') {
tran[i] = 1;
}
if (s[i] == 'G') {
tran[i] = 3;
}
if (s[i] == 'T') {
tran[i] = 2;
}
}
map<int,int>hash;
int sum = 0;
int i = 0;
while (i < 9) {
sum = (sum << 2) | (tran[i]);
i++;
}
for (; i < s.length(); i++) {
sum = ((sum & 0x3ffff) << 2) | (tran[i]);
map<int,int>::iterator it = hash.find(sum);
if (it == hash.end()) {
hash[sum] = 1;
} else {
result[s.substr(i - 9, 10)] = s.substr(i - 9, 10);
}
}
vector<string>ans;
map<string,string>::iterator it = result.begin();
while (it != result.end()) {
ans.push_back(it -> second);
it++;
}
return ans;
}
};