[Leetcode] Repeated DNA sequences

Source: Internet
Author: User

This link has a great discussion on this problem. If you are refer to it. In fact, the "idea" and code in this passage are from the former link.

Well, there are a very intuitive solution to this problem. That's, starting from the first letter of the string, extract a substring of length , check whether it has OCC Urred and not been added to the result. If So, add it to the result; Otherwise, visit the next letter and repeat the above process. However, a naive implementation of this idea would give the MLE error, and this is the real obstacle of the proble M.

Then we need to save spaces. Instead of keeping the whole substring, can is convert it to other formats? Well, you had noticed that there is only 4 letters A, T, C, G in the substring. If we assign each letter 2 bits, then a -letter substring would only cost $ bits and can thus Be accommodated by a -bit integer, greatly lowering the space complexity.

Then you could put this idea into code and get a simple Accepted solution as follows. congratulations!

1 classSolution {2  Public:3vector<string> findrepeateddnasequences (strings) {4unordered_map<int,int>MP;5vector<string>Res;6         inti =0, code =0;7          while(I <9)8Code = ((Code <<2) | Mapping (s[i++]));9          for(; I < (int) s.length (); i++) {TenCode = ((Code <<2) &0xfffff) |mapping (S[i])); One             if(mp[code]++ = =1) ARes.push_back (S.substr (i-9,Ten)); -         } -         returnRes; the     } - Private: -     intMappingChars) { -         if(s = ='A')return 0; +         if(s = ='C')return 1; -         if(s = ='G')return 2; +         if(s = ='T')return 3; A     } at};

Do you see the logic in the above code? Well, we first Merge 9 letters into code . Then, each time we meet a new letter, we merge it to code by & 0xfffff (5 hexadecimal digits). Thus we have a code for the current 10 - Letter substring. We Check whether it has occurred exactly for once to decide whether to push it to the result or not.

The above code can still is shorten using tricks from the above link. In fact, if we code A, T, C, G using 3 bits, the code would be as short as ten lines! Refer to the above link to learn more!

[Leetcode] 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.