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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code