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 — #1316 - Distinct Echo Substrings - LeetCode JavaScript Solutions

Back to all solutions

#1316 - Distinct Echo Substrings

Hard • View on LeetCode • View on GitHub
String Trie Hash Function Rolling Hash

Problem Description

Return the number of distinct non-empty substrings of text that can be written as the concatenation of some string with itself (i.e. it can be written as a + a where a is some string).

Solution

/**
 * @param {string} text
 * @return {number}
 */
var distinctEchoSubstrings = function(text) {
  const set = new Set();

  for (let size = 1; size <= text.length / 2; size++) {
    let count = 0;

    for (let left = 0, right = size; right < text.length; left++, right++) {
      if (text[left] === text[right]) {
        count++;
      } else {
        count = 0;
      }

      if (count === size) {
        set.add(text.slice(left - size + 1, right + 1));
        count--;
      }
    }
  }

  return set.size;
};
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.