easy

Find the Index of the First Occurrence

Given a text string and a non-empty pattern, return the index of the first occurrence of the pattern in the text, or -1 if the pattern does not occur.

Constraints
  • 1 ≤ haystack.length, needle.length ≤ 10^4
  • Lowercase English letters
Examples
in: haystack = "sadbutsad", needle = "sad"
out: 0
in: haystack = "leetcode", needle = "leeto"
out: -1

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.