Super simple HashSet solution, beat 80%


  • 0

    There is no need to use hashmap I think, it's a waste of space.

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

Log in to reply
 

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