Distinct Echo Substrings - Problem

Given a string text, return the number of distinct non-empty substrings that can be written as the concatenation of some string with itself.

In other words, count all substrings that can be written as a + a where a is some non-empty string.

For example, if text = "abcabc", then "abcabc" can be written as "abc" + "abc", so it counts as an echo substring.

Input & Output

Example 1 — Basic Case
$ Input: text = "abab"
Output: 2
💡 Note: Echo substrings are "abab" ("ab" + "ab") and "baba" doesn't exist, but "ab" appears at positions 0-1, and full "abab" is "ab"+"ab". Also "ba" at position 1-2 forms "baba" but that's not in the string. Actually, we have "ab" and "abab" as valid echoes.
Example 2 — Longer String
$ Input: text = "aabaaba"
Output: 4
💡 Note: Echo substrings: "aa" (positions 0-1), "aaba" ("aa"+"ba"), "abaa" ("ab"+"aa"), and "baab" ("ba"+"ab")
Example 3 — No Echoes
$ Input: text = "a"
Output: 0
💡 Note: Single character cannot form an echo substring (need at least length 2)

Constraints

  • 1 ≤ text.length ≤ 2000
  • text consists of lowercase English letters only

Visualization

Tap to expand
Distinct Echo Substrings: Finding a+a patternsababInput: "abab""ab""ab""ab" + "ab" = "abab" ✓"a""a""a" + "a" ≠ in string ✗Result: 2 distinct echo substrings found
Understanding the Visualization
1
Input
String text with potential echo patterns
2
Process
Find all substrings of form a+a where a is non-empty
3
Output
Count of distinct echo substrings
Key Takeaway
🎯 Key Insight: An echo substring has even length with identical first and second halves
Asked in
Google 15 Facebook 12
23.4K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen