Algorithms and Data Structures

Substring Search

Given a long text t and a string s, you are asked to find all the occurrences of s in the text t. You are asked to find the starting positions in t that match s.


The first line of the input contains the text t (1 ≤ |t| ≤ ).
The second line of the input contains the string s (1 ≤ |s| ≤ |t|).


For each occurrence of s in t, output the starting position in t where it occurs. The positions should be in ascending order (from the beginning of the text to the end) separated by a space. The indexing starts from 0.


habababohabo ba
2 4
barfoobarfoobarfoobarfoobarfoo foobarfoo
3 9 15 21


  1. habababohabo → habababohabo, habababohabo
  1. barfoobarfoobarfoobarfoobarfoo → barfoobarfoobarfoobarfoobarfoo, barfoobarfoobarfoobarfoobarfoo, barfoobarfoobarfoobarfoobarfoo, barfoobarfoobarfoobarfoobarfoo


Time limit: 2.5 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in