#187
Medium Algorithms Repeated dna sequences
Hash Table String Bit Manipulation Sliding Window Rolling Hash Hash Function
52.9% acceptance
Jan 12, 2026
3609
570
The DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T'.
For example, "ACGAATTCCG" is a DNA sequence.
When studying DNA, it is useful to identify repeated sequences within the DNA.
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. You may return the answer in any order.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn find_repeated_dna_sequences(s: String) -> Vec<String> {
if s.len() < 10 {
return vec![];
}
let mut seen = std::collections::HashSet::new();
let mut repeated = std::collections::HashSet::new();
for i in 0..=s.len() - 10 {
let substring = &s[i..i + 10];
if !seen.insert(substring) {
repeated.insert(substring.to_string());
}
}
repeated.into_iter().collect()
}
}