28. Find the Index of the First Occurrence in a String
Asked at Amazon, Bloomberg
Difficulty
Easy
Time
O(n * m)
Space
O(1)
Pattern
Two Pointers
Companies
2
Problem
You are given two strings: a long one called haystack and a short one called needle. Find the first position where needle appears inside haystack.
A position means the starting index in haystack. The characters of needle must appear there in the same order, with no gaps and no extra characters in between. If needle never appears, return -1.
For example, if haystack = "sadbutsad" and needle = "sad", the answer is 0 because the match starts at index 0. Even though "sad" also appears at index 6, we want the first one.
Find the first matching start index
Constraints
- ●
1 <= haystack.length, needle.length <= 10^4 - ●
haystack and needle consist of only lowercase English characters
Examples
haystack = "sadbutsad", needle = "sad"0haystack = "leetcode", needle = "leeto"-1Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Amazon | Easy | View all Amazon questions → |
| Bloomberg | Easy | View all Bloomberg questions → |
How to Think About It
Brute force: check every substring of haystack of length needle
Slide a window of size needle across haystack and compare characters
Optimize with Knuth-Morris-Pratt (KMP) by precomputing a failure function on needle
Z-algorithm builds a Z-array to find all occurrences in O(n+m)
In interviews, implement brute force first then discuss KMP if asked for optimal
Optimal Approach
The brute force approach iterates through each position in haystack and checks if the substring starting at that position matches needle character by character. For each starting index i, compare haystack[i..i+len(needle)-1] with needle. If all characters match, return i. If no match is found after checking all positions, return -1. This runs in O(n*m) time where n is haystack length and m is needle length. For optimal performance, KMP precomputes a longest prefix suffix (LPS) array for needle in O(m) time, then scans haystack in O(n) time, achieving O(n+m) overall.
Slide a window of size m
What Trips People Up in Real Interviews
Clarify edge cases: empty needle returns 0, needle longer than haystack returns -1
Mention that brute force is O(n*m) but KMP reduces to O(n+m)
Do not use built-in string find methods unless interviewer allows it
Explain the LPS (longest proper prefix which is also suffix) array in KMP step by step
Practice writing KMP from memory as it is a common follow-up
Solution Code
O(n * m) timedef strStr(haystack, needle):
if not needle:
return 0
n, m = len(haystack), len(needle)
for i in range(n - m + 1):
if haystack[i:i + m] == needle:
return i
return -1Frequently Asked Questions
What is the Find the Index of the First Occurrence in a String problem?
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. This classic string matching problem tests your understanding of pattern matching algorithms and brute-force vs optimized approaches.
How do you solve Find the Index of the First Occurrence in a String?
The optimal approach runs in O(n * m) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Find the Index of the First Occurrence in a String?
Find the Index of the First Occurrence in a String is asked at Amazon, Bloomberg. It is a easy difficulty problem.
What are common mistakes on Find the Index of the First Occurrence in a String?
- Clarify edge cases: empty needle returns 0, needle longer than haystack returns -1
- Mention that brute force is O(n*m) but KMP reduces to O(n+m)
- Do not use built-in string find methods unless interviewer allows it
- Explain the LPS (longest proper prefix which is also suffix) array in KMP step by step
- Practice writing KMP from memory as it is a common follow-up