Leetcode brushing record-28. Implement strStr ()

Insert picture description here

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        
        lenh = len(haystack)
        lenn = len(needle)
        if lenh == lenn and haystack == needle:
            return 0
        elif lenh < lenn:
            return -1
        checkrange = lenh - lenn + 1
        for i in range(checkrange):
            if haystack[i:i+lenn] == needle:
                return i
        else:
            return -1
Published 59 original articles · Liked 14 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/weixin_41545780/article/details/105478992