题解:Repeated DNA Sequences

xiaoxiao2021-02-28  105

题目如下:

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.

For example,

Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", Return: ["AAAAACCCCC", "CCCCCAAAAA"].

解决代码如下:

vector<string> findRepeatedDnaSequences(string s) { vector<string> result; unordered_map<string, int> t; if(s.length() <= 10) return result; for(int i = 0; i < s.length() - 9; i++) { string count = s.substr(i, 10); if(t.find(count) == t.end()) t[count] = 1; else t[count]++; } for(auto it : t) { if(it.second > 1) result.push_back(it.first); } return result; }
转载请注明原文地址: https://www.6miu.com/read-63546.html

最新回复(0)