FunctionRacerProblemsRace

Find the Index of the First Occurrence

EasyStringsPython, JavaScript, TypeScript

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems