#K63227. Substring Search
Substring Search
Substring Search
Given two strings, your task is to determine the starting index of the second string (substring) in the first string (main string). In other words, find the index i such that
\(s_1[i,i+|s_2|-1]=s_2\)
where \(|s_2|\) is the length of the substring. If s_2
is an empty string, the answer is defined to be 0. If s_2
is not a substring of s_1
, output -1
.
Please note that the index is 0-based.
inputFormat
The input consists of two lines. The first line contains the main string s_1
. The second line contains the substring s_2
to search for.
If the second line is missing or empty, it should be treated as an empty string.
outputFormat
Output a single integer which is the starting index at which s_2
occurs in s_1
. Output -1
if s_2
is not a substring of s_1
.
hello
ell
1