Bit Manipulation


  • 0
    N
    public List<String> findRepeatedDnaSequences(String s) {
        if(s == null || s.length()==0) return new ArrayList<String>();
        HashMap<Character,Integer> map = new HashMap<Character,Integer>();
        map.put('A',0);
        map.put('C',1);
        map.put('G',2);
        map.put('T',3);
        HashSet<Integer> set = new HashSet<Integer>();
        HashSet<String> twice = new HashSet<String>();
        // Encode String to number
        for(int i=0;i<s.length()-9;i++){
            int code = 0;
            for(int j=i;j<i+10;j++){
                code <<= 2;
                code |= map.get(s.charAt(j));
            }
            if(set.contains(code)){
                set.add(code);
                twice.add(s.substring(i,i+10));
            }else{
                set.add(code);
            }
        }
        return new ArrayList<String>(twice);
    }

Log in to reply
 

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