Note that in Python the
count () function returns the number of substrings in a given string, but it does not produce correct results when two occurrences of a substring overlap. Consider this example —
The output we got here is 2, but the expected result is 3 because we also wanted to count the occurrence of the overlapping substring.
To solve this problem, we can use the
find () function in Python. It returns the starting position of the first occurrence of a substring in a given string, then we increment that position by 1 and continue searching from that position to the end of the string.
Below is the implementation —