Classic problem #187MediumHash TableStringBit ManipulationSliding WindowRolling HashHash FunctionZ AlgorithmBoyer–Moore String-Search Algorithm
Repeated DNA Sequences
Real-world version: Ten-Letter Runs That Come Round AgainTracked in this browser only
Read Repeated DNA Sequences on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
A strand reads s, a string over the four letters 'A', 'C', 'G' and 'T'.
Return every run of exactly ten letters that appears in the strand more than once, each run listed once, in the order they first appear.
Examples
Example 1
- Input
- s = "GGTCAGGTCAGGTCATTTGCA"
- Output
- ["GGTCAGGTCA"]
The run "GGTCAGGTCA" starts at both position 0 and position 5, so it comes round again. No other ten-letter run repeats.
Example 2
- Input
- s = "GATTACAGATTACAGATTACA"
- Output
- ["GATTACAGAT", "ATTACAGATT", "TTACAGATTA", "TACAGATTAC", "ACAGATTACA"]
Two runs repeat here, and they are listed in the order they first appear.
Example 3
- Input
- s = "ACGTACGTAC"
- Output
- []
The strand is exactly ten letters long, so there is only one run and nothing can repeat.
Constraints
- 1 <= s.length <= 10^5
- s[i] is 'A', 'C', 'G' or 'T'
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def find_repeated_dna_sequences(s: str) -> list[str]:Java
public List<String> findRepeatedDnaSequences(String s)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Longest Duplicate SubstringReal-world version: Longest Repeated Stretch on a Log LineHard
- Find Beautiful Indices in the Given Array IReal-world version: Nearby Marker PositionsMedium
- Longest Nice SubstringReal-world version: Longest Matched-Case RunEasy
- Shortest PalindromeReal-world version: Bead Strand MirrorHard