Calculate the number of occurrences of substrings in a string