Find the Index of the First Occurrence in a String - Problem

You are a detective searching for clues! Given two strings, needle and haystack, your mission is to find where the first occurrence of the needle appears within the haystack.

This is a classic string matching problem that appears frequently in coding interviews. You need to return the index (0-based) of the first character where needle starts in haystack. If the needle is not found anywhere in the haystack, return -1.

Example: If haystack = "hello world" and needle = "world", you should return 6 because "world" starts at index 6.

This problem tests your understanding of string manipulation, pattern matching, and algorithm optimization. While it seems simple, there are multiple approaches ranging from brute force to advanced string matching algorithms!

Input & Output

example_1.py โ€” Basic match
$ Input: haystack = "sadbutsad", needle = "sad"
โ€บ Output: 0
๐Ÿ’ก Note: The first occurrence of "sad" in "sadbutsad" starts at index 0, so we return 0.
example_2.py โ€” No match found
$ Input: haystack = "leetcode", needle = "leeto"
โ€บ Output: -1
๐Ÿ’ก Note: "leeto" is not found in "leetcode", so we return -1.
example_3.py โ€” Empty needle
$ Input: haystack = "hello", needle = ""
โ€บ Output: 0
๐Ÿ’ก Note: By convention, an empty string is found at index 0 of any string.

Visualization

Tap to expand
String Pattern Matching Processhaystack = "sadbutsad" | needle = "sad"Step 1: Check position 0sadbutsadโœ“ Match found at position 0!Step 2: If no match, check position 1sadbutsadโœ— Mismatch at third character (d vs b)needle = "sad"Looking for this patternTime Complexity: O(nร—m)n = haystack length, m = needle lengthAlgorithm continues until match found or all positions checkedโœ“Match foundโœ—Mismatch
Understanding the Visualization
1
Position the needle
Try to align the needle with each position in haystack
2
Character comparison
Compare characters from left to right
3
Handle mismatches
When mismatch occurs, move to next position
4
Success condition
All characters match - return the starting position
Key Takeaway
๐ŸŽฏ Key Insight: String matching can be solved with simple brute force O(nร—m) or optimized using built-in functions that implement advanced algorithms like KMP for O(n+m) complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n+m)

Built-in functions typically use optimized algorithms with linear time complexity

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Built-in functions are space-optimized

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค haystack.length, needle.length โ‰ค 104
  • haystack and needle consist of only lowercase English characters
  • needle will not be longer than haystack
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
89.7K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen