Find the Index of the First Occurrence in a String
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.

30:00

Find the Index of the First Occurrence in a String
easy
Topics
Companies

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":"sadbutsad","needle":"sad"}
Output: 0
Constraints:
  • 1haystack.length,needle.length1041 \leq \text{haystack.length}, \text{needle.length} \leq 10^4

  • haystack and needle consist of only lowercase English characters.

Input
arr ={"haystack":"sadbutsad","needle":"sad"}

Find needle in haystack

s
s0a1d2b3u4t5s6a7d8
t
s0a1d2
Variables
VariableValue
haystacksadbutsad
needlesad
DepthFunction Call
Stack empty
0/1