GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #187 - Repeated DNA Sequences - LeetCode JavaScript Solutions

Back to all solutions

#187 - Repeated DNA Sequences

Medium • View on LeetCode • View on GitHub
String Hash Table Bit Manipulation Sliding Window Hash Function Rolling Hash

Problem Description

The DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T':

  • For example, "ACGAATTCCG" is a DNA sequence.

When studying DNA, it is useful to identify repeated sequences within the DNA.

Given a string s that represents a DNA sequence, return all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in any order.

Solution

/**
 * @param {string} s
 * @return {string[]}
 */
var findRepeatedDnaSequences = function(s) {
  let substring = s.slice(0, 10);
  const set = new Set([substring]);
  const result = new Set();

  for (let i = 10; i < s.length; i++) {
    substring = substring.slice(1) + s[i];
    if (set.has(substring)) {
      result.add(substring);
    }
    set.add(substring);
  }

  return [...result];
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.