JAVA O(n) HashSet solution


  • 0
    O
    public class Solution {
        public List<String> findRepeatedDnaSequences(String s) {
            LinkedList<String> list = new LinkedList<String>();
            HashSet<String> set = new HashSet<String>();
            HashSet<String> check = new HashSet<String>();
            for(int i = 0; i <= s.length() - 10; i++){
                int j = i+10;
                String tmp = s.substring(i,j);
                if(set.contains(tmp)){
                    if(!check.contains(tmp)) {
                        list.add(tmp);
                        check.add(tmp);
                    }
                }else{
                    set.add(tmp);
                }
            }
            return list;
        }
    }
    

Log in to reply
 

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