Find the Index of the First Occurrence

Easy

Given two strings haystack and needle, return the index of the first occurrence of needle in haystack, or -1 when it is not part of it.

Example 1

strStr()
Input
haystack = "sadbutsad", needle = "sad"
Output
0

Example 2

strStr()
Input
haystack = "leetcode", needle = "leeto"
Output
-1
Constraints
1 <= haystack.length, needle.length <= 10^4

Topics

StringsTwo Pointers

Implement strStr in the editor and return the answer — printing it is never graded.