[Leetcode] Repeated DNA sequences, problem solving report

Source: Internet
Author: User

Catalogue

    • Directory
    • Objective
    • Topic
    • Native ideas
    • Binary ideas
      • AC

Preface

Recently on the Leetcode on the probability of an AC more and more low, I here is not one time AC can be recorded, the problem-solving ideas to share to everyone.

Topics

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,

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

Native Ideas

To see this topic, my first thought is:

    1. The For loop iterates through the strings, each of which consists of a string of 10 characters, stored in Hashtable or HashMap or HashSet.
    2. Each time you put a string into a hash data structure, you need to determine if the substring is already in the current hash data structure, and if so, you need to save the substring to the list and return as the result.

At the same time, in order to pursue efficiency, I chose hashset. Because Hashtable are thread-synchronized, there is a certain reduction in efficiency. HashMap, however, wastes a portion of the storage space.

With the idea, the code is easy to write:

List<string> reslist =NewArraylist<string> ();if(s = =NULL|| S.length () <=Ten) {returnReslist; } set<string> sets =NewHashset<string> (); for(inti =0; I <= s.length ()-Ten; i++) {String key = s.substring (i, i +Ten);if(Sets.contains (key) &&! Reslist.contains (Key))            {Reslist.add (key); }Else{Sets.add (key); }        }returnReslist;

But the result is not satisfactory, or time-out.

One of the good things about Leetcode is that you can participate in the discussion. Whenever I can't come up with a plan the first time, I will also refer to the discussion, as the practice of English. Through the discussion of this topic, we have the following binary ideas.

Binary Ideas

Through the study of Dicsuss discussion, it was found that the above native method timed out because the string storage wasted too much space and time, so consider using integer storage, the binary method. This idea is very simple, there is a total of four letters: A,c,g,t. Our idea of converting integers is as follows:

    1. A = 00,c = 01,g = 10,t = 11.
    2. int key = 0, key = key << 2 | Code (a| c| G| T).

So that we can easily convert a string to an integer, the above formula is not clear, it is possible to look directly at the conversion code:

    Private Static int hashcode(String str) {inthash =0; for(inti =0; I < str.length (); i + +) {hash = hash <<2|        Mapinteger (Str.charat (i)); }returnHash }Private Static int Mapinteger(CharCH) {Switch(CH) { Case ' A '://xx            return 0; Case ' C '://            return 1; Case ' G '://Ten            return 2; Case ' T ':// One            return 3;default:return 0; }    }

Knowing how str turns into integers, the following idea is the same as before native, nothing more than the previous HashSet storage is a string, and now the storage is an integer.

AC
 Public  class solution {     Public StaticList<string>findrepeateddnasequences(String s) {List<string> reslist =NewArraylist<string> ();if(s = =NULL|| S.length () <=Ten) {returnReslist; } set<integer> Set =NewHashset<integer> (); for(inti =0; I <= s.length ()-Ten; i + +) {String substr = s.substring (i, i +Ten);intKey = Hashcode (SUBSTR);if(Set.contains (key) &&!reslist.contains (substr))            {Reslist.add (substr); }Else{Set.add (key); }        }returnReslist; }Private Static int hashcode(String str) {inthash =0; for(inti =0; I < str.length (); i + +) {hash = hash <<2|        Mapinteger (Str.charat (i)); }returnHash }Private Static int Mapinteger(CharCH) {Switch(CH) { Case ' A '://xx            return 0; Case ' C '://            return 1; Case ' G '://Ten            return 2; Case ' T ':// One            return 3;default:return 0; }    }}

[Leetcode] Repeated DNA sequences, problem solving report

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.