MediumNo. 0187
Repeated DNA Sequences — LeetCode 187 Python Solution
- Pattern
- Sliding Window
- Topics
- Bit Manipulation, Hash Table, String, Sliding Window, Hash Function, Rolling Hash
- Reading time
- 2 min
- Source
- leetcode.com
The problem
The DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T'. For example, "ACGAATTCCG" is a DNA sequence.
Example
- Input
- s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
- Output
- ["AAAAACCCCC","CCCCCAAAAA"]
Python solution
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 ansComplexity
- Time
- O(n × 10)
- Space · auxiliary
- O(n × 10)
Pattern: Sliding Window
Collapse a nested loop over every subarray into a single pass with two indices. LeetCode 187. Repeated DNA Sequences is filed here because LeetCode tags it Sliding Window, which is the vocabulary this hub collects.
The sliding window guide has the Python template for the pattern and the 116 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1461 | Check If a String Contains All Binary Codes of Size K | Medium |
| 1016 | Binary String With Substrings Representing 1 To N | Medium |
| 1763 | Longest Nice Substring | Easy |
| 0003 | Longest Substring Without Repeating Characters | Medium |
| 0030 | Substring with Concatenation of All Words | Hard |
| 0076 | Minimum Window Substring | Hard |
Frequently asked questions
- How hard is LeetCode 187. Repeated DNA Sequences?
LeetCode 187. Repeated DNA Sequences is rated Medium on LeetCode.
- What is the time complexity of LeetCode 187. Repeated DNA Sequences?
The Python solution on this page runs in O(n × 10).
- What is the space complexity of LeetCode 187. Repeated DNA Sequences?
The Python solution on this page uses O(n × 10) auxiliary space.
- What topics does LeetCode 187. Repeated DNA Sequences cover?
LeetCode 187. Repeated DNA Sequences is tagged Bit Manipulation, Hash Table, String, Sliding Window, Hash Function and Rolling Hash on LeetCode.