zoukankan      html  css  js  c++  java
  • LeetCode-187 Repeated DNA Sequences

    题目描述

    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.

    题目大意

    给定一个字符串,寻找字符串中长度为10的并且出现不止一次的子串。

    示例

    E1

    Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
    
    Output: ["AAAAACCCCC", "CCCCCAAAAA"]

    解题思路

    利用unordered_map保存不同子串出现的次数,最后遍历map查找符合条件的子串。

    复杂度分析

    时间复杂度:O(n)

    空间复杂度:O(n)

    代码

    class Solution {
    public:
        vector<string> findRepeatedDnaSequences(string s) {
            vector<string> res;
            if(s.length() < 10)
                return res;
            
            unordered_map<string, int> m;
            string subs = s.substr(0, 10);
            m[subs]++;
            //记录不同子串出现的次数
            for(int i = 10; i < s.length(); i++) {
                subs.erase(0, 1);
                subs += s[i];
                m[subs]++;
            }
            
            unordered_map<string, int>::iterator iter = m.begin();
            //遍历寻找出现多次的子串
            for(; iter != m.end(); iter++) {
                if(iter->second > 1)
                    res.push_back(iter->first);
            }
            
            return res;
        }
    };
  • 相关阅读:
    androidlayout_weight的使用
    软件开发中的真理.
    apk,task,android:process与android:sharedUserId的区别
    WIFI连接
    go simple web server
    echo命令
    shell if
    linux grep命令(包括正则)
    make命令和Makefile文件
    linux中grep命令的用法
  • 原文地址:https://www.cnblogs.com/heyn1/p/10979284.html
Copyright © 2011-2022 走看看