• Topic describes

Title link:Leetcode-cn.com/problems/re…

  • Thought analysis

A hash table is used to record the number of occurrences of each DNA sequence (that is, a substring of length ten), and any occurrences of a particular substring more than once are counted in the answer. So just loop once, take the first character of the current subscript each time, and put the substring of length 10 into the hash table. If the value of the substring is 2, it can be put into the answer. At the same time, it can avoid putting the same substring into the answer table repeatedly.

  • code

var findRepeatedDnaSequences = function (s) {
  if (s.length <= 10) {
    return [];
  }
  let mmap = new Map(a);let res = [];
  for (let i = 0; i <= s.length - 10; i++) {
    let ss = s.slice(i, 10 + i);
    mmap.set(ss, (mmap.get(ss) || 0) + 1);
    if (mmap.get(ss) == 2) { res.push(ss); }}return res;
};
Copy the code