zoukankan      html  css  js  c++  java
  • 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.

    For example,

    Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",
    
    Return:
    ["AAAAACCCCC", "CCCCCAAAAA"].
    

    Analyse: Use a map to store all 10-letter-long sequences and count their times at the same time. Do a two-pass examination and put all sequences appear more than once in the result vector. 

     1 class Solution {
     2 public:
     3     vector<string> findRepeatedDnaSequences(string s) {
     4         vector<string> result;
     5         if(s.length() < 11) return result;
     6         
     7         unordered_map<string, int> um;
     8         for(int i = 0; i < s.size() - 9; i++){
     9             um[s.substr(i, 10)]++;
    10         }
    11         for(unordered_map<string, int>::iterator ite = um.begin(); ite != um.end(); ite++){
    12             if(ite->second > 1)
    13                 result.push_back(ite->first);
    14         }
    15         return result;
    16     }
    17 };
  • 相关阅读:
    抽象工厂模式
    工厂方法模式
    简单工厂模式
    多例模式
    深入分析 Java 中的中文编码问题
    PipedInputStream和PipedOutputStream详解
    单例模式
    Java IO和NIO文章目录
    wsdlLocation可以写成项目的相对路劲吗
    ssh框架配置事务管理器
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5180884.html
Copyright © 2011-2022 走看看