leetcode[187]repeated DNA Sequences

Source: Internet
Author: User

All DNA are composed of a series of nucleotides abbreviated as a, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it's 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,

" AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT " , return:["aaaaaccccc""cccccaaaaa"].
classSolution { Public:/** * All DNA is composed of a series of bases, respectively, ACGT, the topic requires the identification of all substrings of length 10, these substrings must be greater than 1 times in the original string (recurring) * Ideas: * 1, the violent enumeration will definitely time out * 2, hash * 1) unordered_set<string> repeated to store substrings of length 10, traverse strings, find s[i]~s[i+9] in repeated:
* If not found, then add it to the repeated, if found, repeat, add it to vector<string> res; * 2) However, the unordered_set<string> will consume a lot of storage space for the extra-long input string; * Improved: String compression (10 char substring requires 8bit*10=80bit, while a C G T four character requires two bit bit encoding 00 01 10 11, 10 char characters required 2bit*10=20bit,1 int=32 bit) * 3) also need to consider the duplicate answer in Res, because each time as long as the presence of repeated in the res, which will obviously cause the problem of repeated placement; * Improved: A unordered_set<int> check is constructed to store the Strint value corresponding to the repeating substring that has been deposited in res; * */Vector<string> findrepeateddnasequences (strings) {vector<string>Res; if(S.empty () | | s.size () <Ten)returnRes; Unordered_map<Char, unsignedint> SMAP = {{'A',0},{'C',1},{'G',2},{'T',3}}; Unordered_set<unsignedint>repeated, check; intStrint =0; for(inti =0; I <Ten; i++) {Strint= (strint<<2) +Smap[s[i]]; } repeated.insert (Strint); for(inti =Ten; I < s.size (); i++) {Strint= ((Strint &0x3ffff) <<2)+Smap[s[i]]; if(Repeated.find (strint) = =Repeated.end ()) {Repeated.insert (strint); }Else{ if(Check.find (strint) = =Check.end ()) {Res.push_back (S.substr (i-9,Ten)); Check.insert (Strint); } } } returnRes; }};

leetcode[187]repeated DNA Sequences

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.