Easy understand and straightforward java solution


  • 19
    J
    public class Solution {
        public List<String> findRepeatedDnaSequences(String s) {
            List<String> res = new ArrayList<String>();
            Set<String> resset = new HashSet<String>();
            if(s == null || s.length() <= 10){
                return res;
            }
            Set<String> set = new HashSet<String>();
            int len = s.length();
            for(int i = 0; i <= len - 10; i++){
                String sub = s.substring(i, i + 10);
                if(!set.add(sub)){
                    resset.add(sub);
                }
            }
            res.addAll(resset);
            return res;
        }
    }
    

    my idea is to get all the possible 10 letter long sequences and put them into set, it the operation failed, it means there are duplicates. so put the sequence into another set("AAAAAAAAAAAA" could have three "AAAAAAAAAA" sequences, so this set will remove the duplicates) then add all the set to the final list.


  • 3
    E

    Upvote your solution. I had the same idea except that I use ArrayList to check if my result has duplicate. I tested your solution and mine, the result is beat 95% vs poorly 25%......So instantiating a new set to help is really good for program efficiency!


Log in to reply
 

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