Skip to content

Commit

Permalink
Create 1698.Number-of-Distinct-Substrings-in-a-String_v2.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Dec 27, 2020
1 parent c107334 commit 9abc5a9
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int countDistinct(string s)
{
int n = s.size();
long base = 26;
long M = 1e9+9;
int count = 1;
long power = 1;
long hash0 = 0;

for (int len = 1; len <=n-1; len++)
{
hash0 = (hash0 * base % M + s[len-1]-'a') % M;
power = (power * base) % M;

unordered_set<long>Set({hash0});
long hash = hash0;
for (int i=len; i<n; i++)
{
hash = (hash * base % M + (s[i]-'a')) % M;
hash = (hash - power*(s[i-len]-'a') % M + M) %M ;
Set.insert(hash);
}
count += Set.size();
}
return count;
}
};

0 comments on commit 9abc5a9

Please sign in to comment.