Problem 209429 · easy · Phase 02 Linear Data Structures

Count Overlapping Occurrences

strings · pattern matching · iteration

Write count_overlapping(s, sub) that counts how many times sub occurs in s, including overlapping occurrences. Matching is case-sensitive.

Examples

Input:  s = "aaaa", sub = "aa"
Output: 3
Explanation: positions 0, 1 and 2.

Input:  s = "abababa", sub = "aba"
Output: 3

Input:  s = "hello", sub = "z"
Output: 0

Constraints

  • 0 <= len(s) <= 10**5, 1 <= len(sub) <= 100, printable ASCII
  • Note that s.count(sub) does not count overlaps

Goals

  • Search for a substring starting from a given index
  • Advance by one to allow overlaps
  • Handle patterns longer than the text
Starting Python…