easy +10 pts

Find all indexes of a substring

Return every starting index where a substring appears, including overlapping matches.

Write a function `find_all_indexes(text: str, substring: str) -> list[int]` that returns a list of all starting indices (in ascending order) where `substring` occurs in `text`. Overlapping occurrences must be counted. For example, in text `'aaa'` and substring `'aa'`, the matches start at indices 0 and 1. If `substring` is empty, return an empty list. If no matches are found, return an empty list. The function must be case-sensitive.

Constraints

Input strings consist of printable ASCII characters. Length of `text` is between 0 and 1000. Length of `substring` is between 0 and 100. Expected time complexity: O(len(text) * len(substring)) or better.

Example

>>> find_all_indexes('hello world', 'o')
[4, 7]
>>> find_all_indexes('ababa', 'aba')
[0, 2]
>>> find_all_indexes('test test', 'test')
[0, 5]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

You can use a loop that advances one character at a time and checks if `text.startswith(substring, i)`.
Remember to allow overlapping by incrementing the index by 1 after each found match.
If `substring` is empty, there is no valid match—return an empty list immediately.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.