Repeated DNA Sequences

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Repeated DNA Sequences相关的知识,希望对你有一定的参考价值。

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"].

 

Analyse: Use a map to store all 10-letter-long sequences and count their times at the same time. Do a two-pass examination and put all sequences appear more than once in the result vector. 

 1 class Solution {
 2 public:
 3     vector<string> findRepeatedDnaSequences(string s) {
 4         vector<string> result;
 5         if(s.length() < 11) return result;
 6         
 7         unordered_map<string, int> um;
 8         for(int i = 0; i < s.size() - 9; i++){
 9             um[s.substr(i, 10)]++;
10         }
11         for(unordered_map<string, int>::iterator ite = um.begin(); ite != um.end(); ite++){
12             if(ite->second > 1)
13                 result.push_back(ite->first);
14         }
15         return result;
16     }
17 };

 

以上是关于Repeated DNA Sequences的主要内容,如果未能解决你的问题,请参考以下文章

Repeated DNA Sequences

187 Repeated DNA Sequences 重复的DNA序列

Repeated DNA Sequences

LeetCode-187. Repeated DNA Sequences

187. Repeated DNA Sequences

187. Repeated DNA Sequences