181. Find All Occurrences of a Substring

EasyStringString

Given a string s and a pattern pat, find all starting indices where pat occurs in s.

Overlapping occurrences should also be included.

Return the list of indices as a comma-separated list inside square brackets, e.g. [0,3,6].

Input: Two comma-separated strings: s and pat.

Output: A list of starting indices (sorted ascending) as [i,j,...], or [] if none found.

Examples

Example 1
Input: abcabcabc,abc
Output: [0,3,6]
Explanation: 'abc' starts at indices 0, 3, and 6.
Example 2
Input: aaaa,aa
Output: [0,1,2]
Explanation: Overlapping: 'aa' at 0,1,2.
Example 3
Input: hello,xyz
Output: []
Explanation: 'xyz' not found in 'hello'.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →