187. Repeated DNA Sequences

195 篇文章 0 订阅
问题描述

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

Example:

Input: s = “AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”
Output: [“AAAAACCCCC”, “CCCCCAAAAA”]

题目链接:


思路分析

给一个string代表DNA序列,找到窗口长度为10的在s中国出现超过1次的子串。

利用map来在循环中统计每一个子串出现的次数,然后遍历map,将出现次数大于1的存入res中。

代码
class Solution {
public:
    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> res;
        if (s.length() <= 10)
            return res;
        unordered_map<string, int> map;
        for (int i = 0; i <= s.length() - 10; i++){
            string temp = s.substr(i, 10);
            map[temp]++;
        }
        for (auto it = map.begin(); it != map.end(); it++){
            if (it->second > 1)
                res.push_back(it->first);
        }
        return res;
    }
};

时间复杂度: O(n2)
空间复杂度: O(n)


反思

看到python的解法感觉很是简洁。利用collection库计数,利用列表解析式生成结果。果然是要玩转这些第三方库才能横着走啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值