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
Zaloguj się, aby zobaczyć historię rozwiązań
Zaloguj sięZaloguj się, aby korzystać z Mentora AI
Zaloguj sięVisible tests
1
Input
"hello", "l"Expected
22
Input
"abababab", "ab"Expected
43
Input
"aaaa", "aa"Expected
24
Input
"abc", "x"Expected
05
Input
"mississippi", "ss"Expected
26
Input
"aaaa", "abc"Expected
0Focus radio
Paused · SomaFM · Fluid