class Solution:
def findRepeatedDnaSequences(self, s: str) -> List[str]:
ans = []
cnt = defaultdict(int)
for i in range(len(s) - L + 1):
sub = s[i : i + L]
cnt[sub] += 1
if cnt[sub] == 2:
ans.append(sub)
return ans
class Solution:
def findRepeatedDnaSequences(self, s: str) -> List[str]:
n = len(s)
if n <= L:
return []
ans = []
x = 0
for ch in s[:L - 1]:
x = (x << 2) | bin[ch]
cnt = defaultdict(int)
for i in range(n - L + 1):
x = ((x << 2) | bin[s[i + L - 1]]) & ((1 << (L * 2)) - 1)
cnt[x] += 1
if cnt[x] == 2:
ans.append(s[i : i + L])
return ans
2024/11/8
class Solution: def findRepeatedDnaSequences(self, s: str) -> List[str]: ans = [] cnt = defaultdict(int) for i in range(len(s) - L + 1): sub = s[i : i + L] cnt[sub] += 1 if cnt[sub] == 2: ans.append(sub) return ans
作者:力扣官方题解 链接:https://leetcode.cn/problems/repeated-dna-sequences/solutions/1035568/zhong-fu-de-dnaxu-lie-by-leetcode-soluti-z8zn/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
L = 10 bin = {'A': 0, 'C': 1, 'G': 2, 'T': 3}
class Solution: def findRepeatedDnaSequences(self, s: str) -> List[str]: n = len(s) if n <= L: return [] ans = [] x = 0 for ch in s[:L - 1]: x = (x << 2) | bin[ch] cnt = defaultdict(int) for i in range(n - L + 1): x = ((x << 2) | bin[s[i + L - 1]]) & ((1 << (L * 2)) - 1) cnt[x] += 1 if cnt[x] == 2: ans.append(s[i : i + L]) return ans
作者:力扣官方题解 链接:https://leetcode.cn/problems/repeated-dna-sequences/solutions/1035568/zhong-fu-de-dnaxu-lie-by-leetcode-soluti-z8zn/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。```