> For the complete documentation index, see [llms.txt](https://joshualbarb.gitbook.io/leetcode-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://joshualbarb.gitbook.io/leetcode-problems/strings/string-simulation/implement-strstr.md).

# Implement strStr()

## Problem

Implement [strStr()](http://www.cplusplus.com/reference/cstring/strstr/).

Return the index of the first occurrence of needle in haystack, or `-1` if `needle` is not part of `haystack`.

{% hint style="info" %}
For example:

```
Input: haystack = "hello", needle = "ll"
Output: 2
```

```
Input: haystack = "aaaaa", needle = "bba"
Output: -1
```

```
Input: haystack = "", needle = ""
Output: 0
```

{% endhint %}

## Solution

```
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        
        length = len(needle)
        if needle == "":
            return 0
        
        for i in range(len(haystack)):
            if haystack[i:i+length] == needle:
                return i
        return -1
        
#Time: O(n)
#Space: O(1)

#Its just substring matching
```
