124. Implement strStr()
Easy · String
Implement the strStr() function to find the first occurrence of a needle string within a haystack string. Return the index of the first occurrence, or -1 if the needle is not found in the haystack.
This is a classic string matching problem. You may use built-in string search methods, or implement your own matching algorithm.
Examples
Example 1 Input: haystack = "hello", needle = "ll" Output: 2 Explanation: "ll" first appears at index 2 in "hello".
Example 2 Input: haystack = "aaaa", needle = "bbb" Output: -1 Explanation: "bbb" does not appear in "aaaa", so return -1.
Constraints
- Standard input/output constraints apply