Java hashSet solution


  • 0
    L
    public List<String> findRepeatedDnaSequences(String s) {
        Set<String> cache = new HashSet<>();
        Set<String> res = new HashSet<>();
        for(int i = 0; i <= s.length()-10; i++){
            String cur = s.substring(i,i+10);
            if(cache.contains(cur)) res.add(cur);
            else cache.add(cur);
        }
        List<String> result = new ArrayList<>(res);
        return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.