---
# System prepended metadata

title: '**Leetcode筆記(Find the Index of the First Occurrence in a String)**'
tags: [easy, string, microsoft, leetcode]

---

# **Leetcode筆記(Find the Index of the First Occurrence in a String)**


:::info
:information_source: 題目 : Find the Index of the First Occurrence in a String, 類型 : string , 等級 : easy
日期 : 2023/12/10，2024/04/02
:::


### 嘗試
```python
class Solution(object):
    def strStr(self, haystack, needle):
        l = len(needle)
        for i in range(len(haystack)):
            if haystack[i : i + l] == needle:
                return i
        return -1
```
2024/04/02
```python
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        for i in range(len(haystack) - len(needle) + 1):
            if needle == haystack[i : i + len(needle)]:
                return i
        
        return -1
```

---
### **優化**

```python


```
---



**思路**


**講解連結**

Provided by. 
