Clean easy understand solution in java


  • 0
    C
    public class Solution {
        public List<String> findRepeatedDnaSequences(String s) {
            List<String> res = new ArrayList<String>();
            HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
            int[] digit = new int[256];
            digit['C' - 'A'] = 1;
            digit['G' - 'A'] = 2;
            digit['T' - 'A'] = 3;
            int rHash = 0;
            int mask = 0xFFFFF;
            for(int i = 0; i < s.length(); i++){
                rHash = (rHash << 2) + (digit[s.charAt(i) - 'A']);
                if(i < 9) continue;
                if(map.containsKey(rHash & mask) ){
                    if(map.get(rHash & mask) == 1)
                        res.add(s.substring(i - 9, i + 1));
                    map.put(rHash & mask, map.get(rHash & mask) + 1);
                } else{
                    map.put(rHash & mask, 1);
                }
            }
            return res;
        }
    }

Log in to reply
 

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