题目概述:给定一字符串,返回该字符串中出现次数大于等于2且长度为10的子串
解题思路:由于其给定了子串的长度,所以我们利用substring函数直接枚举即可
时间复杂度:\(O(n)\)
代码:
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
Set<String>set = new HashSet<>();
Map<String,Integer>map = new HashMap<>();
if(s.length() <= 10)return new ArrayList<String>(set);
for(int i = 0; i <= s.length() - 10; i ++){
String sub = s.substring(i,i + 10);
int t = map.getOrDefault(sub,0);
t++;
map.put(sub,t);
if(t >= 2)set.add(sub);
}
return new ArrayList<String>(set);
}
}
标签:子串,set,DNA,重复,字符串,给定,序列,new
From: https://www.cnblogs.com/dengch/p/17815815.html