Sobes.tech
Junior — Senior

Function that returns the positions of a substring within a string

livecode

Task condition

Develop a function that takes two strings — the main string source and the substring pattern. The function should return a list of indices, starting from zero, where pattern occurs in source. If the substring is not found, return an empty list.

Example:

find_occurrences("ababa", "aba")  # → [0, 2]

Requirements:

  • Input data can be any strings, including empty.
  • The search should consider overlapping occurrences.
  • The algorithm should run in linear time relative to the length of source on average.