LeetCode #187 Medium

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.

Constraints
  • 1 <= s.length <= 10⁵
  • s[i] is either 'A', 'C', 'G', or 'T'.
hash-tablestringsliding-windowbit-manipulation
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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).

1

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.

2

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.

3

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def findRepeatedDnaSequences(self, s):
▶3 count = {}
▶4 result = []
▶5 for i in range(len(s) - 9):
▶6 window = s[i:i + 10]
▶7 count[window] = count.get(window, 0) + 1
▶8 if count[window] == 2:
▶9 result.append(window)
▶10 return result
A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countnothing seen yetanswerempty9 windows of 10 letters to check
s18 letters
windows9one per start index
A 10-letter window can start at index 0 up to index 8, so there are 9 windows. Each one gets counted once in the map; a sequence is reported the moment its count reaches 2.
window 0 to 9A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC1answeremptynew window → count 1
i0window start
windowACGTACGTAC
count1
ACGTACGTAC has not been seen before, so its count starts at 1. It is not a repeat yet.
window 1 to 10A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC1CGTACGTACG1answeremptynew window → count 1
i1window start
windowCGTACGTACG
count1
CGTACGTACG has not been seen before, so its count starts at 1. It is not a repeat yet.
window 2 to 11A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC1CGTACGTACG1GTACGTACGT1answeremptynew window → count 1
i2window start
windowGTACGTACGT
count1
GTACGTACGT has not been seen before, so its count starts at 1. It is not a repeat yet.
window 3 to 12A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC1CGTACGTACG1GTACGTACGT1TACGTACGTA1answeremptynew window → count 1
i3window start
windowTACGTACGTA
count1
TACGTACGTA has not been seen before, so its count starts at 1. It is not a repeat yet.
window 4 to 13A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC2CGTACGTACG1GTACGTACGT1TACGTACGTA1answerACGTACGTACsecond copy → add to answer
i4window start
windowACGTACGTAC
count2
ACGTACGTAC was seen once before, so its count becomes 2: the first proof that it repeats. Add it to the answer now.
window 5 to 14A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC2CGTACGTACG2GTACGTACGT1TACGTACGTA1answerACGTACGTACCGTACGTACGsecond copy → add to answer
i5window start
windowCGTACGTACG
count2
CGTACGTACG was seen once before, so its count becomes 2: the first proof that it repeats. Add it to the answer now.
window 6 to 15A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC2CGTACGTACG2GTACGTACGT2TACGTACGTA1answerACGTACGTACCGTACGTACGGTACGTACGTsecond copy → add to answer
i6window start
windowGTACGTACGT
count2
GTACGTACGT was seen once before, so its count becomes 2: the first proof that it repeats. Add it to the answer now.
window 7 to 16A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC2CGTACGTACG2GTACGTACGT2TACGTACGTA2answerACGTACGTACCGTACGTACGGTACGTACGTTACGTACGTAsecond copy → add to answer
i7window start
windowTACGTACGTA
count2
TACGTACGTA was seen once before, so its count becomes 2: the first proof that it repeats. Add it to the answer now.
window 8 to 17A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC3CGTACGTACG2GTACGTACGT2TACGTACGTA2answerACGTACGTACCGTACGTACGGTACGTACGTTACGTACGTAcopy number 3 → already listed
i8window start
windowACGTACGTAC
count3
ACGTACGTAC is already in the answer from its second copy. Its count rises to 3, but it is not added again, so it stays listed once.
A0C1G2T3A4C5G6T7A8C9G10T11A12C13G14T15A16C17window → countACGTACGTAC3CGTACGTACG2GTACGTACGT2TACGTACGTA2answerACGTACGTACCGTACGTACGGTACGTACGTTACGTACGTA4 repeated → return them
result4 sequences
Every window has been counted. The answer holds each sequence whose count reached 2, once each, in the order its second copy appeared.
05

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.

▶1class Solution:
▶2 def findRepeatedDnaSequences(self, s):
▶3 bits = {"A": 0, "C": 1, "G": 2, "T": 3}
▶4 mask = (1 << 20) - 1
▶5 count = {}
▶6 result = []
▶7 code = 0
▶8 for i, ch in enumerate(s):
▶9 code = ((code << 2) | bits[ch]) & mask
▶10 if i >= 9:
▶11 count[code] = count.get(code, 0) + 1
▶12 if count[code] == 2:
▶13 result.append(s[i - 9:i + 1])
▶14 return result
06

Common pitfalls

Adding the sequence on every repeat

✗ Wrong
if count[window] > 1:
    result.append(window)
✓ Right
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

✗ Wrong
for i in range(len(s) - 10):
✓ Right
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++

✗ Wrong
for (int i = 0; i <= s.size() - 10; i++)
✓ Right
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.

07

Edge cases

s shorter than 10 letters, e.g. "ACGT"

There is no full window, the loop range is empty, and the answer is [].

Overlapping copies, e.g. "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.

08

Complexity

Time
O(n)
Space
O(n)
There are n − 9 windows, and cutting and hashing one costs 10 steps, a constant. The map can hold up to n − 9 different sequences. The 2-bit version stores one integer per window instead of a 10-letter string.
09

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.