Implement strStr

Problem Return the index of the first occurrence of needle in haystack, or -1 if absent (empty needle → 0).

Input / Output

  • Input: strings haystack, needle.
  • Output: first match index or -1.

Constraints

  • Lengths up to 510^4; the naive O(nm) sliding compare is acceptable; know a linear-time option.

Example

  • haystack = "sadbutsad", needle = "sad" → 0.
asked …
LeaderboardSalaryAccount
Implement strStr · 2dbi