class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if len(needle) == 0 and len(haystack) == 0 : return 0
if len(haystack) == 0 : return -1
if len(needle) == 0 : return 0
if len(needle) > len(haystack) : return -1
len_n = len(needle)
for i in range(len(haystack)) :
last = i + len_n
if haystack[i:last] == needle :
return i
return -1
개선점
exception 줄이는 것 가능