Repeated DNA Sequences
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Bit Manipulation.
Repeated DNA Sequences
Given a string
s that represents a DNA sequence, return all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.Examples
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC","CCCCCAAAAA"]
Approach 1
Level I: Brute Force (Hash Set of Strings)
Intuition
Iterate through every possible 10-letter substring and store it in a hash set. If we encounter a substring that is already in the set, we add it to our result list.
Thought Process
- Use a
seenhash set to track substrings. - Use a
repeatedhash set to collect result strings. - Loop
ifrom 0 tos.length() - 10:- Extract substring
s.substring(i, i + 10). - If in
seen, add torepeated. - Else, add to
seen.
- Extract substring
Pattern: String Rolling Window
⏱ O(N \cdot L) - Where $N$ is string length and $L=10$ is the sequence length.💾 O(N \cdot L) - To store strings in sets.
Approach 2
Level II: Rolling Hash (Rabin-Karp)
Intuition
Instead of re-extracting substrings, we treat each 10-letter window as a number in base 4 (since there are 4 DNA bases). As we slide the window, we update the hash in time.
⏱ $O(N)$💾 $O(N)$
Approach 3
Level III: Optimal (Bitmasking)
Intuition
Since there are only 4 characters, map them to 2 bits. A 10-char sequence is a 20-bit integer. Use a sliding window to update the bitmask in per character.
Thought Process
- Map A=0, C=1, G=2, T=3.
- Maintain a 20-bit sliding window.
- Use a hash map/set to track encountered bitmasks.
Pattern: 2-bit Character Encoding
⏱ O(N) - Linear scan without string creation.💾 O(min(N, 2^{20})) - Hash set storage for 20-bit integers.
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.