dsa · easy
Index of First Occurrence
Return the starting index of the first place needle appears as a contiguous slice of haystack. If it never appears, return -1.
Arguments
haystack— the text to searchneedle— the slice to find
Example
haystack = "sadbutsad", needle = "sad" → 0 (it also appears later at 6; take the first).
haystack = "practice", needle = "drill" → -1.
Constraints
1 <= haystack.length, needle.length <= 4*10^4 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: "sadbutsad" "sad" Expected: 0
Example 2
Input: "practice" "drill" Expected: -1