Count Overlapping Matches

A regular search can miss matches that start inside a previous match. Given a string s and a substring sub, count how many times sub appears in s, including overlapping occurrences. If sub is empty, return 0.

Примеры

Ввод
"aaaa", "aa"
Вывод
3
Ввод
"abcabc", "abc"
Вывод
2
Ввод
"aaa", ""
Вывод
0

Нужно реализовать

def count_overlapping(s: str, sub: str) -> int:

Теги

stringssliding-window
PostgreSQLv16

Ovdje će se prikazati rezultat izvršavanja koda

Visible tests
1
Input
"aaaa", "aa"
Expected3
2
Input
"abcabc", "abc"
Expected2
3
Input
"aaa", ""
Expected0
4
Input
"abc", "xyz"
Expected0
5
Input
"aaaaa", "aaa"
Expected3
6
Input
"Hello, World!", "aaa"
Expected0
Focus radio
Paused · SomaFM · Fluid