simple Python code


  • 0
    L
    class Solution(object):
        def strStr(self, haystack, needle):
            if len(needle) == 0:
                return 0
            if len(haystack) < len(needle):
                return -1
            i = 0
            while True:
                if haystack[i] == needle[0]:
                    if len(needle) > len(haystack)-i:
                        return -1
                    elif haystack[i:i+len(needle)] == needle:
                        return i
                i += 1
                if i == len(haystack):
                    return -1

Log in to reply
 

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