6-line 35ms Java, 2 sets


  • 1
    A
    public List<String> findRepeatedDnaSequences(String s) {
        Set<String> dna = new HashSet<>(), res = new HashSet<>();
        for (int i = 10; i <= s.length(); i++) {
            String d = s.substring(i-10,i);
            if (!dna.add(d)) res.add(d);
        }
        return new ArrayList<String>(res);
    }

Log in to reply
 

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