[Leetcode] 187 Repeated DNA Sequences, leetcoderepeated

Source: Internet
Author: User

[Leetcode] 187 Repeated DNA Sequences, leetcoderepeated

(1) The first practice is to use map <string, int> to record the number of strings with 10 characters. If it exceeds 2, push_back is entered into ans. But MLE indicates that using string is not a good method.

Below is the MLE code:

class Solution {public:    vector<string> findRepeatedDnaSequences(string s) {    vector <string>  ans;        map<string,int> mp;        if(s.length()<10)        return ans;        for(int i=0;i<s.length()-10;i++)        mp[s.substr(i,10)]++;        map<string,int>::iterator it;        for(it=mp.begin();it!=mp.end();++it)        {        if(it->second>1)        ans.push_back(it->first);        }        return ans;    }};
(2) After reading the Tags, I am prompted to use the bitwise operation. This reminds me of the uniqueness of the prefix code of the Hoffmann code, so the following mark can be used here:

A: 00 T: 01 C: 10G: 11 A total of 10 characters, A total of 20 characters, and an int has 32 characters, so map <int, int> processing can reduce space usage.

The clock maintains a 20-bit space. During traversal, the first character is removed from the left 14-bit space, and the first character is removed from the right 12-bit space, in this way, it has played a role in saving time.

class Solution {public:    vector<string> findRepeatedDnaSequences(string s) {    vector <string>  ans;        map <int,int> mp;        map <char,int> cur;        set<string> st;        cur['A']=0;        cur['T']=1;        cur['C']=2;        cur['G']=3;        if(s.length()<10)        return ans;        int temp;        for(int i=0;i<9;i++)        {        temp<<=2;        temp|=cur[s[i]];         }       // mp[temp]++;        for(int i=9;i<s.length();i++)        {        temp<<=14;        temp>>=12;        temp|=cur[s[i]];        mp[temp]++;        if(mp[temp]>=2)        st.insert(s.substr(i-9,10));        }        set<string>::iterator it;        for(it=st.begin();it!=st.end();it++)        ans.push_back(*it);                return ans;    }};


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.