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