Leetcode 第28題,這次用slicing string 去做做看
28. Find the Index of the First Occurrence in a String
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.
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.
Example 2:
Input: haystack = "leetcode", needle = "leeto"
Output: -1
Explanation: "leeto" did not occur in "leetcode", so we return -1.
Constraints:
1 <= haystack.length, needle.length <= 104haystackandneedleconsist of only lowercase English characters.
我的作法:


思路 :
- 定義m為haystack長度,n為needle長度
- 若needle 不在haystack中,輸出-1
- 專注看haystack
- 利用for迴圈從haystack一開始往後找needle長度的字串 ,即找haystack[i:i+n]
- 若haystack[i:i+n] = needle,則回傳haystack的字首開頭位置號碼










