Leetcode repeated DNA sequences

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/repeated-dna-sequences/

From the beginning to the length of the string 10, each one after the move, put into the hashset, if the hashset has already, put in the res, while ensuring that res is not duplicated.

This method is time O (n), and n is the length of S. Occupies a set, space O (n).

The quicker way to do this is because there are only four characters, so you can use a string of length 10 to figure out hashcode to HashSet, so you can save time comparing substring strings.

When calculating Hashcode, A is 00; C is 01; G is 10; T is 11. Each hash bit operation left two bits, the latter two become 0, and the new character represents the number to do bit arithmetic or.

AC Java:

1  Public classSolution {2      PublicList<string>findrepeateddnasequences (String s) {3list<string> res =NewArraylist<string>();4         if(s = =NULL|| S.length () < 11){5             returnRes;6         }7         8hashset<integer> HS =NewHashset<integer>();9          for(inti = 0; I<=s.length ()-10; i++){TenString subString = s.substring (i,i+10); One             intHashcode =Gethash (subString); A  -             if(Hs.contains (hashcode) &&!res.contains (subString)) { - Res.add (subString); the}Else{ - Hs.add (hashcode); -             } -         } +         returnRes; -     } +      A     Private intGethash (String s) { atHashmap<character, integer> HM =NewHashmap<character, integer>(); -Hm.put (' A ', 0); -Hm.put (' C ', 1); -Hm.put (' G ', 2); -Hm.put (' T ', 3); -          in         inthash = 0; -          for(inti = 0; I<s.length (); i++){ tohash = Hash << 2 |Hm.get (S.charat (i)); +         } -         returnHash; the     } *}

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.