Leecode no.187 重复的DNA序列

package leecode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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


/**
* 找到s中长度为10的子串,且子串在s中出现 > 1次
* 思路:
* 从第一个元素往后按长度10截取字符串,加入到Map中
* 最后统计Map中那些字符串超过1的
*
* @param s
* @return
*/
public List<String> findRepeatedDnaSequences(String s) {
List<String> list = new ArrayList<>();

if(s.length() <= 10) {
return list;
}

Map<String, Integer> resultMap = new HashMap<>();

//子串起始索引的最大值
int maxIndex = s.length() - 10;

for(int i = 0; i <= maxIndex; i++) {
String childStr = s.substring(i, i+10);
resultMap.put(childStr, resultMap.getOrDefault(childStr, 0) + 1);
}
for (String key : resultMap.keySet()) {
if(resultMap.get(key) > 1) {
list.add(key);
}
}
return list;

}


public static void main(String[] args) {

}




}
posted @ 2021-12-15 15:31  六小扛把子  阅读(29)  评论(0编辑  收藏  举报