Rolling bit code
Time O(n) Space O(n)code = ((code << 2) | letter) & 0xFFFFF. Track seen codes and codes already reported.
import java.util.*;
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
Map<Character, Integer> bits = Map.of('A', 0, 'C', 1, 'G', 2, 'T', 3);
Set<Integer> seen = new HashSet<>(), reported = new HashSet<>();
List<String> out = new ArrayList<>();
int code = 0;
for (int i = 0; i < s.length(); i++) {
code = ((code << 2) | bits.get(s.charAt(i))) & 0xFFFFF;
if (i < 9) continue;
if (!seen.add(code) && reported.add(code)) out.add(s.substring(i - 9, i + 1));
}
return out;
}
}Verdict: A perfect hash: no collisions.