Find the Index of the First Occurrence
Return the index in haystack where the string needle first appears as a contiguous piece, or -1 if it never appears. Do the search yourself with a loop instead of calling find or index. An empty needle is found at index 0.
Example 1
haystack "sadbutsad"
needle "sad"
returns 0
First found at index 0.
Example 2
haystack "leetcode"
needle "leeto"
returns -1
Never appears.
Constraints
0 <= len(haystack), len(needle) <= 100- Lowercase letters only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.