#K37877. Find Substring Indices
Find Substring Indices
Find Substring Indices
You are given two strings: a larger string S and a substring P. Your task is to find all starting indices at which P occurs in S. The indices are 0-indexed. Formally, for each index \(i\) such that \(0 \leq i \leq |S| - |P|\), if \(S[i:i+|P|] = P\) then record index \(i\). If there are no occurrences, output an empty line.
Input Format:
- The first line of input contains the string S.
- The second line contains the substring P.
Output Format:
- Output all starting indices where P occurs in S, separated by a single space. If there are no occurrences, output an empty line.
You may assume that \(|S|\) and \(|P|\) are within reasonable limits so that a simple linear scan is efficient.
inputFormat
The input consists of two lines:
- The first line is the larger string S.
- The second line is the substring P.
outputFormat
Output a single line containing the starting indices (0-indexed) separated by a space where the substring P occurs in S. If P is not found in S, print an empty line.
## sampleababcabc
abc
2 5
</p>