LeetCode-187. Repeated DNA sequences

Source: Internet
Author: User

The problem is to detect the number of repetitions of the substring. It began to take for granted that 10 strings were removed sequentially by s.substring, and then compared by equal. Although the result can be, there is no doubt that time complexity O (n2) timed out.

The first layer of traversal is unavoidable, and the comparison of strings can be optimized. Similar to string problems, you can convert to byte operations. So modify the code as follows:

 Public classSolution { PublicList<string>findrepeateddnasequences (String s) {List<String> resultlist =NewArraylist<>(); The final resultintKey = 0; if(S.length () ==0)        {            returnresultlist; } HashMap<Character,Integer> map =NewHashmap<character, integer>(); Encode four of letters to reduce storage space while speeding up the comparison speed Map.put (' A ', 0); Map.put (' C ', 1); Map.put (' G ', 2); Map.put (' T ', 3); HashMap<Integer,Integer> result =NewHashmap<integer, integer>();  for(intI=0;i<s.length (); i++) {Key= (key<<2 | map.get (S.charat (i)) & 0x3) & 0XFFFFF; The 00.01.10.11 can represent four characters, representing 2 bits, respectively. Each time a left 2-bit, through the &0x3 can be removed after two bits. FFFFF fixed 2*10 bitif(i<9)Continue; if(Result.get (key) = =NULL) {result.put (key,1); } Else if(Result.get (key) = = 1) {Resultlist.add (s.substring (i-9, i + 1)); Result.put (Key,2); }        }        returnresultlist; }}

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.