#K65352. Find the First Occurrence of a Substring
Find the First Occurrence of a Substring
Find the First Occurrence of a Substring
Given two strings, a main string and a substring, your task is to find the index of the first occurrence of the substring in the main string. The search is case-sensitive and only exact matches are considered.
If the substring does not appear in the main string, output -1
.
You need to find the smallest index i (0-indexed) such that:
$$ main\_string[i:i+|sub\_string|] = sub\_string $$
If no such index exists, output \(-1\).
Examples:
- For main string "hello" and substring "ll", the answer is 2.
- For main string "hello" and substring "LL", the answer is -1.
- For main string "abcdabcd" and substring "bc", the answer is 1.
inputFormat
The input is provided via stdin and consists of two lines:
- The first line contains the main string.
- The second line contains the substring to search for.
outputFormat
The output should be printed to stdout and is a single integer: the index (0-indexed) of the first occurrence of the substring in the main string. If the substring is not found, output -1.
## samplehello
ll
2