Problem Description
Given a string text, find the number of distinct non-empty substrings that can be written as a concatenation of a string with itself (i.e. as a + a). For example, if a substring is "abcabc", it is valid because it is formed by "abc" concatenated with "abc". The task is to count each distinct valid substring only once.
Key Insights
- Only even-length substrings can form an echo substring because they need to be split into two equal halves.
- Using a brute force approach (checking all even-length substrings) can be inefficient. A rolling hash is an effective way to quickly compute and compare hash values of substring halves.
- Precomputing hash values and appropriate power values enables constant-time comparisons for any substring.
- Store valid echo substrings (or a hash representation along with their length) in a set to ensure uniqueness.
Space and Time Complexity
Time Complexity: O(n^2) – We iterate over all even-length substrings. Space Complexity: O(n^2) in the worst case due to the hash set storage, plus O(n) for auxiliary arrays.
Solution
Our approach uses rolling hash to compare two halves of every even-length substring in constant time. We start by precomputing a prefix hash array and a power array based on a chosen base (26, for lowercase letters) and modulus (10^9 + 7) to minimize collisions. Then, for every possible even-length substring, we compute the hash of the first half and the hash of the second half. If they are equal, the substring is valid and is added (using a tuple of the hash of the entire substring and its length for uniqueness) to a set. Finally, we return the size of this set.