[LeetCode] Distinct Echo Substrings

1316. Distinct Echo Substrings

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
long long mod = 1e9 + 7, p = 31;
public:
int distinctEchoSubstrings(string text) {
int n = text.length();
unordered_set<long long> us;
vector<long long> pow(n+1,1);
vector<long long> hash(n+1,text[0]);
for(int i = 1; i <= n; i++) {
pow[i] = pow[i-1] * p % mod;
hash[i] = (hash[i-1] + pow[i] * text[i] % mod) % mod;
}
for(int len = 1; len <= n / 2; len++) {
for(int i = 0; i <= n - len - len; i++) {
long long now = (hash[i+len-1] - (i ? hash[i-1] : 0) + mod) % mod;
long long next = (hash[i+len+len-1] - hash[i+len-1] + mod) % mod;
now = now * pow[n-1-i] % mod;
next = next * pow[n-1-i-len] % mod;
if(now != next or us.count(now)) continue;
us.insert(now);
}
}
return us.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/26/PS/LeetCode/distinct-echo-substrings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.