FunctionRacerProblemsRace

Count Overlapping Occurrences

EasyStringsPython, JavaScript, TypeScript, Java, C++

Return how many times pattern occurs in text, counting occurrences that overlap. An occurrence is a start index i such that the characters of text from i to i + len(pattern) - 1 spell pattern; two occurrences may share characters.

For example "aa" occurs 3 times in "aaaa", at indexes 0, 1 and 2. Return 0 when pattern is longer than text.

Example 1

text "aaaa"
pattern "aa"
returns 3

The starts at indexes 0, 1 and 2 all read aa; the built-in count method would say 2 because it skips overlaps.

Example 2

text "abcabcab"
pattern "abc"
returns 2

abc starts at indexes 0 and 3; the final ab is too short to be a third one.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems