Find the Index of the First Occurrence in a String
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack. If needle is not part of haystack, return -1.
Example 1
Input
haystack = "sadbutsad", needle = "sad"Output
0The substring "sad" first appears in "sadbutsad" starting at index 0.
Example 2
Input
haystack = "leetcode", needle = "leeto"Output
-1The substring "leeto" does not appear in "leetcode", so the result is -1.
Constraints
- 1 <= haystack.length, needle.length <= 10^4
- haystack and needle consist of only lowercase English characters.