Repeated DNA Sequences
Repeated DNA Sequences is LeetCode 187 (Medium). A DNA string s uses only the letters A, C, G and T. Return every 10-letter substring that occurs more than once in s, in any order.
- Two occurrences may overlap.
- List each repeated sequence once, however many times it occurs.
s can be 100,000 letters long, so the windows cannot be compared in pairs; each one should be checked once.
- 1 <= s.length <= 10⁵
- s[i] is either 'A', 'C', 'G', or 'T'.
Intuition
A 10-letter window starts at each index from 0 to n − 10, so there are only n − 9 of them. Slide along s and keep a count for every window in a hash map:
- Count becomes 2: this sequence has just repeated for the first time, so add it to the answer.
- Count goes past 2: it is already in the answer, and adding it again would list it twice.
Adding at exactly 2 lists each repeated sequence once, with no second set. The window length is fixed at 10, so cutting and hashing a window is a constant amount of work and the whole scan is linear.
A fixed-length window and the question "which ones appear more than once" is a sliding window feeding a hash map. With a 4-letter alphabet, each window can also be packed into a small integer, which is the classic bit-manipulation follow-up.
Approach
Before reading on, count how many 10-letter windows s has, and decide what you would store for each one so a repeat is spotted in one lookup. Then decide when exactly to add a sequence so it is listed only once. Aim for O(n).
Two ways to solve it
Count each 10-letter substring in a map and report it at count 2.
- Simple: no encoding to get wrong.
- Cost: stores up to n strings of 10 letters.
The usual interview answer.
Pack each window into a 20-bit integer and update it in O(1) per letter.
- Smaller keys: one integer per window instead of a string.
- Exact: different sequences always get different codes.
The follow-up when memory or speed is pressed.
Both make one pass. The hash map wins on clarity, and the window length is a constant 10, so cutting each window costs only a constant. The steps, code and live demo follow the hash map, and the 2-bit version comes after the demo.
Prepare a map of counts and an answer list
The map goes from each 10-letter sequence to the number of times it has appeared so far. It replaces scanning back through s: one lookup tells you whether a window has been seen.
Visit every window once
For each start i from 0 to len(s) − 10, take s[i:i+10]. Every occurrence of a 10-letter sequence starts at one of these indexes, overlapping ones included, so nothing is missed. A string shorter than 10 gives an empty range.
Add the window when its count reaches 2
Add 1 to the window's count. If the new count is exactly 2, append the window to the answer: the second copy is the first proof that it repeats. Once every window is counted, return the list.
Code in Python, C++ and Java
Repeated DNA sequences with a 2-bit code
Write A, C, G, T as 0, 1, 2, 3, two bits each. Ten letters then fill exactly 20 bits. For each new letter, shift the code left by 2, add the letter's bits, and keep only the low 20 bits, so the code always describes the last 10 letters. The map counts codes instead of strings, and a string is cut only when a code reaches count 2.
Common pitfalls
Adding the sequence on every repeat
if count[window] > 1:
result.append(window)if count[window] == 2:
result.append(window)Thirteen As hold four copies of AAAAAAAAAA, so > 1 appends it three times. The answer must list each repeated sequence once.
Stopping one window early
for i in range(len(s) - 10):
for i in range(len(s) - 9):
The last window starts at len(s) − 10, and range stops before its end value. With eleven As the windows start at 0 and 1; the short loop checks only the first and returns [] instead of ["AAAAAAAAAA"].
Subtracting from an unsigned size in C++
for (int i = 0; i <= s.size() - 10; i++)
for (int i = 0; i + 10 <= (int)s.size(); i++)
s.size() is unsigned, so for s = "ACGT" the value 4 - 10 wraps to a huge number and the loop keeps going until substr is called past the end and throws. Adding to i instead of subtracting from the size never wraps.
Edge cases
s shorter than 10 letters, e.g. "ACGT"There is no full window, the loop range is empty, and the answer is [].
"ACACACACACACAC"ACACACACAC starts at 0, 2 and 4, and CACACACACA at 1 and 3. Copies that share letters still count as separate occurrences, so both sequences are returned.
Complexity
Repeated DNA Sequences FAQ
Why does the 2-bit code use the mask 0xFFFFF?
0xFFFFF is twenty 1-bits, room for exactly 10 letters at 2 bits each. After the shift, the oldest letter sits above bit 19 and the mask cuts it off. A 16-bit mask 0xFFFF keeps only 8 letters, so AAAAAAAAAA and CCAAAAAAAA would get the same code and be counted as one sequence.
Can two different sequences share a 2-bit code?
No. Each letter has its own 2-bit pattern and its own position in the 20 bits, so the code can be read back into the 10 letters it came from. That is why this version needs no collision check, unlike a general rolling hash such as Rabin-Karp.