Skip to content

Commit faf4014

Browse files
Update string-algorithms.md
1 parent 623380b commit faf4014

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

‎string-algorithms.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ vector<vector<int>> group_identical_strings(vector<string> const& s)
390390
391391
![](.gitbook/assets/image%20%2863%29%20%281%29.png)
392392
393-
### Number of different substrings in a string \(N^2 log N\)
393+
### Number of different substrings or Number of unique substring in a string \(N^2 log N\)
394394
395395
```cpp
396396
int count_unique_substrings(string const& s)
@@ -421,6 +421,7 @@ int count_unique_substrings(string const& s)
421421
}
422422
return cnt;
423423
}
424+
// There's a better way using LCP & Suffix Array below.
424425
```
425426

426427
## Rolling Hashes and Bloom Filters
@@ -629,8 +630,24 @@ vector<int> constructLCP(string const& s, vector<int> const& p) // string s
629630
}
630631
```
631632

632-
### Count number of different substrings
633+
### Count number of different substrings or unique substrings
633634
```c++
635+
/*
636+
Calculate suffix array and LCP array first.
637+
Example: "ABABBAB"
638+
SA String LCP
639+
5 AB 0
640+
0 ABABBAB 2
641+
2 ABBAB 2
642+
6 B 0
643+
4 BAB 1
644+
1 BABBAB 3
645+
3 BBAB 1
646+
647+
A substring is a prefix of a suffix. So problem now is calculate unique prefixes across suffixes.
648+
For n chars it will be n*(n+1)/2
649+
There will be LCP[i] duplicates at every suffix level
650+
*/
634651
intnumberOfDiffSubstrings(vector<int> &lcp, string &s)
635652
{
636653
int n = s.size();

0 commit comments

Comments
(0)