LeetCode 187. Repeated DNA Sequences
考点 | 难度 |
---|---|
Hash Map | Medium |
题目
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.
思路
用dictionary记录sequence和出现的次数。
答案
class Solution:
# @param s, a string
# @return a list of strings
def findRepeatedDnaSequences(self, s):
table = collections.defaultdict(int)
for i in range(len(s) - 9):
table[s[i: i+10]] += 1
return map(lambda x: x[0], filter(lambda x: x[1] > 1, table.items()))