Skip to content
BytePatterns

Repeated DNA Sequences

MediumStrings#rolling-hash#hash-set~30m

Problem

A DNA strand is a string over the letters A, C, G and T. Return every length-10 substring that occurs more than once in the strand, sorted, with each such substring listed only once no matter how often it repeats.

Examples

Input:  dna = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC", "CCCCCAAAAA"]
Why:    both windows appear twice inside the strand
Input:  dna = "AAAAAAAAAAAA"
Output: ["AAAAAAAAAA"]
Why:    the same window slides along and repeats
Input:  dna = "ACGT"
Output: []
Why:    edge case, the strand is shorter than one window

Hints

0 / 3

Stuck on the idea rather than the code? Rabin-Karp Rolling Hash covers it.