---
# System prepended metadata

title: 'Implement strStr() <img src="https://img.shields.io/badge/-easy-brightgreen">'
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Implement strStr() <img src="https://img.shields.io/badge/-easy-brightgreen">

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.

<ins>**Example 1:**</ins>

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

<ins>**Example 2:**</ins>

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

**Clarification:**

For the purpose of this problem, we will return 0 when *needle* is an empty string.

## Solution

```python=
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        n = len(needle)
        if n == 0:
            return 0
        
        for i in range(len(haystack)):
            if i <= len(haystack)-n and haystack[i:i+n] == needle:
                return i
        return -1
```