Talentd
FleetCode

1316. Distinct Echo Substrings

Hard53.7% acceptance1 company

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).

Example 1:

Input: text = "abcabcabc"
Output: 3
Explanation: The 3 substrings are "abcabc", "bcabca" and "cabcab".

Example 2:

Input: text = "leetcodeleetcode"
Output: 2
Explanation: The 2 substrings are "ee" and "leetcodeleetcode".

Constraints:

  • 1 <= text.length <= 2000
  • text has only lowercase English letters.

Asked at 1 company

Hints open one at a time

Hint 1
Given a substring of the text, how to check if it can be written as the concatenation of a string with itself ?
Hint 2
We can do that in linear time, a faster way is to use hashing.
Hint 3
Try all substrings and use hashing to check them.
27:57
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def distinctEchoSubstrings(self, text: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"abcabcabc"
Input
"leetcodeleetcode"
Your code is saved on this device.Solution