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