Skip to content
Onsite

Implement strStr()

EasyP2Coding

Return the index of the first occurrence of needle in haystack, or -1 if needle does not occur. If needle is empty, return 0.

Example 1

Input
haystack = "sadbutsad", needle = "sad"
Output
0

Example 2

Input
haystack = "leetcode", needle = "leeto"
Output
-1

Example 3

Input
haystack = "abc", needle = ""
Output
0

Constraints

  • 0 <= haystack.length, needle.length <= 10^4
  • Both strings consist of lowercase letters.

Hints

Run checks the 3 visible tests, Submit all 12.