> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/zhong-yu-shua-dao-100-dao-le-wo-shi-fen-shui-ling/bu-chong-120-dao/28.-implement-strstr.md).

# 28. Implement strStr()

\# Easy

{% hint style="danger" %}
In order to be more efficiency, only compare elements of `strStr()` in `[0: len(strStr)-len(needle)+1]`
{% endhint %}

{% tabs %}
{% tab title="Python" %}

```python
class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        # edge case
        if len(needle) == 0:
            return 0
        
        # regular case
        i = 0
        j = 0
        flag = -1   # the elements are not same
        while i < len(haystack)-len(needle)+1:
            if haystack[i] == needle[0]:
                x = i+1
                y = 1
                while x < len(haystack) and y < len(needle):
                    if haystack[x] == needle[y]:
                        x += 1
                        y += 1
                    else:
                        break
                if y == len(needle):
                    return i
            i += 1
            
        return -1
```

{% endtab %}
{% endtabs %}
