Find the Index of the First Occurrence in a String
Problem statement
Return the zero-based index of the first occurrence of needle in haystack. Return -1 when no occurrence exists. An empty needle occurs at index 0.
Function
findFirstOccurrence(haystack: String, needle: String) → intExamples
Example 1
haystack = "sadbutsad"needle = "sad"return = 0The first match starts at index 0.
Constraints
- Both strings contain at most
10^5lowercase English letters.