Description
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.
Example:
Input:
s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output:
["AAAAACCCCC", "CCCCCAAAAA"]
分析
题目的意思是:给定一个字符串,找出子字符串出现次数超过一次的子字符串,并且输出。
代码
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_set<string> res,st;
for(int i=0;i+9<s.length();i++){
string t=s.substr(i,10);
if(st.count(t)){
res.insert(t);
}else{
st.insert(t);
}
}
return vector<string>(res.begin(),res.end());
}
};