Skip to content

Commit 84821c6

Browse files
authored
change set to unordered_set making distinct substrings O(n^2)
Wasn't sure why we used set rather than unordered_set to store unique hashes. This should lower the runtime from O(n^2*log(n)) to O(n^2).
1 parent 2b27ef6 commit 84821c6

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/string/string-hashing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ By doing this, we get both the hashes multiplied by the same power of $p$ (which
142142
Here are some typical applications of Hashing:
143143

144144
* [Rabin-Karp algorithm](rabin-karp.md) for pattern matching in a string in $O(n)$ time
145-
* Calculating the number of different substrings of a string in $O(n^2 \log n)$ (see below)
145+
* Calculating the number of different substrings of a string in $O(n^2)$ (see below)
146146
* Calculating the number of palindromic substrings in a string.
147147

148148
### Determine the number of different substrings in a string
@@ -173,7 +173,7 @@ int count_unique_substrings(string const& s) {
173173

174174
int cnt = 0;
175175
for (int l = 1; l <= n; l++) {
176-
set<long long> hs;
176+
unordered_set<long long> hs;
177177
for (int i = 0; i <= n - l; i++) {
178178
long long cur_h = (h[i + l] + m - h[i]) % m;
179179
cur_h = (cur_h * p_pow[n-i-1]) % m;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy