Given a string text, find the number of distinct echo substrings.
An echo substring is a non-empty substring that can be written as the concatenation of some string with itself. In other words, it can be written as a + a where a is some string.
Example: In the string "abcabc", the substring "abcabc" is an echo substring because it equals "abc" + "abc". Similarly, "aa" is an echo substring in "baab" because it equals "a" + "a".
Your task is to count how many distinct echo substrings exist in the given text. Two echo substrings are considered the same if they have identical characters in the same order.
Input & Output
Visualization
Time & Space Complexity
O(n) positions ร O(n) expansions, but with O(1) hash comparisons
Storing distinct echo substrings in the set
Constraints
-
1 โค
text.lengthโค 2000 -
textconsists of lowercase English letters only - Echo substring must have even length (since it's a+a)
- All substrings must be non-empty