public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
// the problem requires to find all substring that appear more than once
// use a hashset to keep adding the substring, if the set already has the string
// add that string to the result hashset
HashSet<String> hs = new HashSet<String>();
// avoid duplicates (*)
HashSet<String> set_ret = new HashSet<String>();
List<String> ret = new ArrayList<>();
if (s.length() < 10) return ret;
for (int i=0; i<=s.length()-10; i++) {
String tmp = s.substring(i, i+10);
if (!hs.add(tmp))
set_ret.add(tmp);
}
ret.addAll(set_ret);
return ret;
}
}
Leetcode 187. Repeated DNA Sequences
最新推荐文章于 2020-10-01 10:16:24 发布