Gary Lai
  • Home
  • About
Sign in Subscribe

187. Repeated DNA Sequences

Last updated on  Jan 29, 2023

187. Repeated DNA Sequences

  1. 使用 Hash Table 的特性來判斷是否有重複出現的 sequence
  2. 滑動窗口的方式來找
class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        table = defaultdict(int)
        for i in range(len(s) - 9):
            table[s[i:i+10]] += 1
        ans = []
        for key, val in table.items():
            if val >= 2:
                ans.append(key)
        return ans
Previous 485. Max Consecutive Ones
Next 380. Insert Delete GetRandom O(1)
Gary Lai © 2025
  • Sign up
Powered by Ghost