DNA序列 由一系列核苷酸组成,缩写为 'A', 'C', 'G' 和 'T'.。
例如,"ACGAATTCCG" 是一个 DNA序列 。
在研究 DNA 时,识别 DNA 中的重复序列非常有用。
给定一个表示 DNA序列 的字符串 s ,返回所有在 DNA 分子中出现不止一次的 长度为 10 的序列(子字符串)。你可以按 任意顺序 返回答案。
示例 1:
输入:s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
输出:["AAAAACCCCC","CCCCCAAAAA"]
示例 2:
输入:s = "AAAAAAAAAAAAA"
输出:["AAAAAAAAAA"]
提示:
0 <= s.length <= 105
s[i]=='A'、'C'、'G' or 'T'
code:
public class FindRepeatedDnaSequences {
public List<String> findRepeatedDnaSequences(String s) {
List<String> res = new ArrayList<>();
if (s == null || s.length() <= 10) {
return res;
}
int len = s.length();
Map<String, Integer> cntMap = new HashMap<>();
for (int i = 0; i < len - 9; i++) {
String str1 = s.substring(i, i + 10);
cntMap.put(str1, cntMap.getOrDefault(str1, 0) + 1);
}
for (Map.Entry<String, Integer> entry : cntMap.entrySet()) {
if (entry.getValue() >= 2) {
res.add(entry.getKey());
}
}
return res;
}
public static void main(String[] args) {
FindRepeatedDnaSequences solution = new FindRepeatedDnaSequences();
List<String> res = solution.findRepeatedDnaSequences("AAAAAAAAAAA");
for (String x : res) {
System.out.println(x);
}
}
}