LeetCode-187 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.
题目大意
给定一个字符串,寻找字符串中长度为10的并且出现不止一次的子串。
示例
E1
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" Output: ["AAAAACCCCC", "CCCCCAAAAA"]
解题思路
利用unordered_map保存不同子串出现的次数,最后遍历map查找符合条件的子串。
复杂度分析
时间复杂度:O(n)
空间复杂度:O(n)
代码
class Solution { public: vector<string> findRepeatedDnaSequences(string s) { vector<string> res; if(s.length() < 10) return res; unordered_map<string, int> m; string subs = s.substr(0, 10); m[subs]++; //记录不同子串出现的次数 for(int i = 10; i < s.length(); i++) { subs.erase(0, 1); subs += s[i]; m[subs]++; } unordered_map<string, int>::iterator iter = m.begin(); //遍历寻找出现多次的子串 for(; iter != m.end(); iter++) { if(iter->second > 1) res.push_back(iter->first); } return res; } };