Skip to content
本页目录

0187-重复的DNA序列

https://leetcode.cn/problems/repeated-dna-sequences

所有 DNA 都由一系列缩写为 'A','C','G' 和 'T' 的核苷酸组成,例如:"ACGAATTCCG"。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。 编写一个函数来找出所有目标子串,目标子串的长度为 10,且在 DNA 字符串 s 中出现次数超过一次。

示例 1:

输入:s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
输出:["AAAAACCCCC","CCCCCAAAAA"]

示例 2:

输入:s = "AAAAAAAAAAAAA"
输出:["AAAAAAAAAA"]

提示:

0 <= s.length <= 105
s[i] 为 'A'、'C'、'G' 或 'T'

思路

使用 Hash 表缓存10个 DNA 序列的个数 最后取出 值大于 1个的 key

参考代码

csharp
public class Solution {
    public IList<string> FindRepeatedDnaSequences(string s) {
    	List<string> list = new List<string>();
    	Dictionary<string,int> dict = new Dictionary<string,int>();
    	if(s.Length > 10){
    		for(int i=0; i<=s.Length -10 ; i++){
    			string key = s.Substring(i,10);
    			if(!dict.ContainsKey(key)){
    				dict.Add(key,1);
    			}
    			else{
    				dict[key]++;
    			}
    		}
    	}
    	//输出
    	foreach(string key in dict.Keys){
    		if(dict[key] > 1){
    			list.Add(key);
    		}
    	}

    	return list;
    }
}

Released under the MIT License.