> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find the Index of the First Occurrence in a

> Tested Python solution for LeetCode 28 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 28, [Easy](/catalog/easy). Topics: [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string), [String Matching](/catalog/topics/string-matching). [View on LeetCode](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 28   # by problem number
lcpy gen -s find_the_index_of_the_first_occurrence_in_a_string   # by problem name
```

## 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`.

### Examples

```
Input: haystack = "sadbutsad", needle = "sad"
Output: 0
Explanation: "sad" occurs at index 0 and 6.
The first occurrence is at index 0, so we return 0.
```

```
Input: haystack = "leetcode", needle = "leeto"
Output: -1
Explanation: "leeto" did not occur in "leetcode", so we return -1.
```

### Constraints

* 1 \<= haystack.length, needle.length \<= 10^4
* haystack and needle consist of only lowercase English characters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_index_of_the_first_occurrence_in_a_string/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_index_of_the_first_occurrence_in_a_string/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m)
    # Space: O(m)
    def str_str(self, haystack: str, needle: str) -> int:
        m = len(needle)
        if m == 0:
            return 0

        # Build KMP failure table: fail[i] = length of longest proper prefix
        # of needle[:i+1] that is also a suffix
        fail = [0] * m
        k = 0
        for i in range(1, m):
            while k > 0 and needle[i] != needle[k]:
                k = fail[k - 1]
            if needle[i] == needle[k]:
                k += 1
            fail[i] = k

        # Scan haystack using failure table to skip re-matched characters
        k = 0
        for i, ch in enumerate(haystack):
            while k > 0 and ch != needle[k]:
                k = fail[k - 1]
            if ch == needle[k]:
                k += 1
            if k == m:
                return i - m + 1
        return -1
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) | O(m) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.