?LeetCode刷題實(shí)戰(zhàn)187:重復(fù)的DNA序列
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.
題意
示例
示例 1:
輸入:s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
輸出:["AAAAACCCCC","CCCCCAAAAA"]
示例 2:
輸入:s = "AAAAAAAAAAAAA"
輸出:["AAAAAAAAAA"]
提示:
0?<= s.length <= 105
s[i] 為 'A'、'C'、'G'?或 'T'
解題
class?Solution?{
public:
??vector<string> findRepeatedDnaSequences(string?s) {
????vector<string> result;
????unordered_map<string, int> myMap;//用于關(guān)聯(lián)各個(gè)長(zhǎng)度為10的子串出現(xiàn)的次數(shù)
????int?strSize = s.size();
????for?(int?beginIndex = 0; beginIndex <= strSize - 10; ++beginIndex) {
??????string?tempRes = s.substr(beginIndex, 10);
??????if?(++myMap[tempRes] == 2) {//第一次出現(xiàn)兩次,避免重復(fù)
????????result.push_back(tempRes);
??????}
????}
????return?result;
??}
};
