Count the number of substrings

I would like to ask if there is an algorithm for counting the number of discrete substring events in a string in O (n) time.

+6
algorithm pattern-matching
source share
2 answers

[EDIT 11/17/2013: count the leaf nodes. Thanks Vinicius Pinto!]

You can build an entity tree

+8
source share

You can use KMP Algorithm and change it to increase the counter instead of returning.

Another possibility is the Rabin-Karp algorithm , however, it depends on hashing, so you either have to accept the possibility of false positives while maintaining complexity is linear or allows the possibility of quadratic complexity, keeping the results 100% correct.

+3
source share

All Articles