Simple Java Solution


  • 0
    H
    public List<String> findRepeatedDnaSequences(String s) {
            List<String> results = new ArrayList<>();
            if(s==null || s.length() < 10) {
                return results;
            }
            Set<String> visited = new HashSet<>();
            Set<String> repeated = new HashSet<>();
            for(int index=0;index <= s.length() - 10; index++) {
                String value = s.substring(index, index+10);
                if(visited.contains(value)) {
                    repeated.add(value);
                }
                visited.add(value);
            }
            results.addAll(repeated);
            return results;
        }
    

  • 0
    P

    I think there is a small bug in the above for loop.

    it should
    for(int i = 0; i<s.length()-9; i++)

    Else it fails for this input ["AAAAAAAAAAA"]


Log in to reply
 

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