Find The Index of The First Occurrence in a String

Easy

Company Tags

You are given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "neetcodeneetcode", needle = "neet"

Output: 0

Explanation: "neet" occurs at index 0 and 8.
The first occurrence is at index 0, so we return 0.

Example 2:

Input: haystack = "neetcode", needle = "codem"

Output: -1

Constraints:

  • 1 <= haystack.length, needle.length <= 10,000
  • haystack and needle consist of only lowercase English characters.


Company Tags

Please upgrade to NeetCode Pro to view company tags.

haystack =

needle =