Substring Count

Counting substrings is useful when looking for repeated fragments in text or code. Write a function count_substring that receives a string s and a substring sub. Return the number of non-overlapping occurrences of sub in s. For example, in aaaa, the substring aa appears twice because the matched positions must not overlap.

Примеры

Ввод
"hello", "l"
Вывод
2
Ввод
"abababab", "ab"
Вывод
4
Ввод
"aaaa", "aa"
Вывод
2

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

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

Теги

strings
PostgreSQLv16

Εδώ θα εμφανιστεί το αποτέλεσμα εκτέλεσης του κώδικα

Visible tests
1
Input
"hello", "l"
Expected2
2
Input
"abababab", "ab"
Expected4
3
Input
"aaaa", "aa"
Expected2
4
Input
"abc", "x"
Expected0
5
Input
"mississippi", "ss"
Expected2
6
Input
"aaaa", "abc"
Expected0
Focus radio
Paused · SomaFM · Fluid