Repeated DNA Sequences
Find every 10-letter stretch of a DNA string that appears more than once, using a set of the stretches seen so far.
A DNA string is made of the letters A, C, G and T. Given a DNA string ss
Example 1
Output: ["AAAAACCCCC", "CCCCCAAAAA"]
Example 2
Output: ["AAAAAAAAAA"]
Explanation: the sequence starts at positions 0, 1, 2 and 3, but is reported once.
The Idea
DNA holds the instructions of a living cell, written as a long chain of four building blocks. Biologists write them as the letters A, C, G and T, so a piece of DNA becomes a string such as AAAAACCCCCAAAAACCCCC...
Take Example 1. Its string has 32 letters, numbered 0 to 31. A stretch of 10 letters can start at position 0, 1, 2 and so on, up to position 22, where the last 10 letters begin. That makes 23 stretches. Most of them appear only once, but two of them appear twice. Look at the picture below.
The orange brackets above the letters mark AAAAACCCCC at 0AAAAACCCCC at 10CCCCCAAAAA at 5CCCCCAAAAA at 16
So how do we notice that the stretch at 10 is one we met before? Reading left to right, we reach position 10 having already passed the stretches at 0 to 9. If we write each stretch down as we pass it, we only need to ask one question at position 10: is AAAAACCCCC
That is the whole idea: walk along the string once, remember every 10-letter stretch you pass, and a stretch you already remember is a repeat. A frame of fixed size that moves one letter at a time like this is called a sliding window.
Solution Approach
Turning that idea into code means settling three things: how to remember the stretches so the lookup is fast, how to spot a repeat, and how to report a stretch only once when it appears three or more times. An optional last part shows a smaller way to store each stretch.
Remembering the stretches
Why not compare every stretch with every other stretch? For Example 1 that is 253 pairs of 23 stretches, and the number of pairs grows with the square of the string's length. For a string of 10,000 letters it is almost 50 million pairs, each a comparison of up to 10 letters.
A HashSetseens.substring(i, i + 10)
Spotting a repeat
How does the code know the stretch was already there? seen.add(window)truefalsefalse
The window must stop where the string stops. The last stretch starts 10 letters before the end, so the loop runs while i + 10 <= s.length()
Reporting each sequence once
What happens in Example 2? Its 13 A's hold four stretches, and all four are AAAAAAAAAAseenfalsefalserepeated
Optional: a number instead of a substring
Cutting out each stretch with substring
Moving the window one letter to the right becomes arithmetic: shift the number two bits left, put the new letter's two bits at the end, and drop the bits of the letter that fell off. In Java that is code = ((code << 2) | letter) & 0xFFFFF0xFFFFF
Java Solution
The method findRepeatedDnaSequences
- First, create two empty sets of strings:
for every stretch met so far, andseen for the stretches met more than once.repeated - Move a start position
along the string with a loop that runs whilei , so the last 10 letters are the last stretch.i + 10 <= s.length() - Inside the loop, cut out the stretch that starts at
asi , withwindow .s.substring(i, i + 10) - Add
towindow . Ifseen returnsadd , the stretch was there already, so add it tofalse .repeated - When the loop ends, copy
into a list and return it.repeated
The main
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RepeatedDNASequences {
public static List<String> findRepeatedDnaSequences(String s) {
Set<String> seen = new HashSet<>();
Set<String> repeated = new HashSet<>(); // a set, so each sequence is reported once
for (int i = 0; i + 10 <= s.length(); i++) {
String window = s.substring(i, i + 10); // the 10 letters starting at i
if (!seen.add(window)) { // add returns false: seen before
repeated.add(window);
}
}
return new ArrayList<>(repeated);
}
public static void main(String[] args) {
System.out.println(findRepeatedDnaSequences("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"));
System.out.println(findRepeatedDnaSequences("AAAAAAAAAAAAA")); // 13 A's
System.out.println(findRepeatedDnaSequences("AAAAAAAAAAA")); // 11 A's
System.out.println(findRepeatedDnaSequences("ACGTACGTAC")); // exactly 10 letters
System.out.println(findRepeatedDnaSequences("ACGT")); // shorter than 10
}
}
Running it prints:
[AAAAACCCCC, CCCCCAAAAA]
[AAAAAAAAAA]
[AAAAAAAAAA]
[]
[]
The first line finds AAAAACCCCCCCCCCAAAAAAAAAAAAAAAseen.addfalserepeated[AAAAAAAAAA, AAAAAAAAAA, AAAAAAAAAA]
The third line comes from 11 A's. They hold only two stretches, at positions 0 and 1, and the two share nine letters, yet they still count as a repeat. The last two lines are empty: 10 letters give one stretch with nothing to match, and 4 letters give none, so the loop never runs.
The original implementation,
Complexity
The work is one pass of the window, with one substring and one or two set operations at each position.
- Time: O(10 × n), where n is the length of
. The loop visits about n start positions, and at each ones copies 10 letters and the set hashes them. With the 20-bit number from the optional part, each step is a few arithmetic operations, so the time is O(n).substring - Space: O(10 × n), because
may hold up to n - 9 different stretches of 10 letters each.seen is never larger thanrepeated .seen