Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
The DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T'.
"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.
Example 1:
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" Output: ["AAAAACCCCC","CCCCCAAAAA"]
Example 2:
Input: s = "AAAAAAAAAAAAA" Output: ["AAAAAAAAAA"]
Constraints:
1 <= s.length <= 105s[i] is either 'A', 'C', 'G', or 'T'.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Bit Manipulation · Sliding Window
"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
"AAAAAAAAAAAAA"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #187: Repeated DNA Sequences
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
Map<String, Integer> cnt = new HashMap<>();
List<String> ans = new ArrayList<>();
for (int i = 0; i < s.length() - 10 + 1; ++i) {
String t = s.substring(i, i + 10);
if (cnt.merge(t, 1, Integer::sum) == 2) {
ans.add(t);
}
}
return ans;
}
}
// Accepted solution for LeetCode #187: Repeated DNA Sequences
func findRepeatedDnaSequences(s string) (ans []string) {
cnt := map[string]int{}
for i := 0; i < len(s)-10+1; i++ {
t := s[i : i+10]
cnt[t]++
if cnt[t] == 2 {
ans = append(ans, t)
}
}
return
}
# Accepted solution for LeetCode #187: Repeated DNA Sequences
class Solution:
def findRepeatedDnaSequences(self, s: str) -> List[str]:
cnt = Counter()
ans = []
for i in range(len(s) - 10 + 1):
t = s[i : i + 10]
cnt[t] += 1
if cnt[t] == 2:
ans.append(t)
return ans
// Accepted solution for LeetCode #187: Repeated DNA Sequences
use std::collections::HashMap;
impl Solution {
pub fn find_repeated_dna_sequences(s: String) -> Vec<String> {
if s.len() < 10 {
return vec![];
}
let mut cnt = HashMap::new();
let mut ans = Vec::new();
for i in 0..s.len() - 9 {
let t = &s[i..i + 10];
let count = cnt.entry(t).or_insert(0);
*count += 1;
if *count == 2 {
ans.push(t.to_string());
}
}
ans
}
}
// Accepted solution for LeetCode #187: Repeated DNA Sequences
function findRepeatedDnaSequences(s: string): string[] {
const n = s.length;
const cnt: Map<string, number> = new Map();
const ans: string[] = [];
for (let i = 0; i <= n - 10; ++i) {
const t = s.slice(i, i + 10);
cnt.set(t, (cnt.get(t) ?? 0) + 1);
if (cnt.get(t) === 2) {
ans.push(t);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.