Easy Java Solution using Set


  • 0
    S
    public class Solution {
        public List<String> findRepeatedDnaSequences(String s) {
            List<String> list= new ArrayList();
            if(s==null || s.length() <10)
                return list;
            int start=0, end =10;
            Set<String> set = new HashSet();
            Set<String> set2 = new HashSet();
            
            while(end<=s.length()){
                
                String str=s.substring(start,end);
                if(!set.contains(str)) 
                    set.add(str);
                else
                    set2.add(str);
                
                start++;
                end++;
            }
            list.addAll(set2);
            return list;
        }
    }

Log in to reply
 

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