Simple Python solution

  • 0
    class Solution(object):
        def strStr(self, haystack, needle):
            :type haystack: str
            :type needle: str
            :rtype: int
            if len(needle) == 0:
                return 0
            if len(needle) > len(haystack):
                return -1
            h, n = len(haystack), len(needle)
            for i in range(h-n+1):
                if haystack[i:i+n] == needle:
                    return i
            return -1

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.