Practice Find the Index of the First Occurrence in a String
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Easy#28
LeetCode ProblemFind the Index of the First Occurrence in a String
Problem Statement
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.
Source: Find the Index of the First Occurrence in a String on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Input
haystack = "sadbutsad", needle = "sad"
Output
0
# Example 2
Input
haystack = "leetcode", needle = "leeto"
Output
-1
Constraints
1 <= haystack.length, needle.length <= 10^4
haystack and needle consist of only lowercase English characters.
Related LeetCode Problems
Practice more problems similar to Find the Index of the First Occurrence in a String.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.