Could you tell me why python code won't pass using the same algorithm in java?

  • 0

    The algorithm passed by using both Java and Javascript, but why this code won't pass for the test case:

    "a", "a"

    It will return -1, instead of 0.

    I guess there might be some grammar mistake, can some one point it out?


    class Solution:
    # @param {string} haystack
    # @param {string} needle
    # @return {integer}
    def strStr(self, haystack, needle):
        if len(haystack) < len(needle):
            return -1
        if (len(haystack) == 0) or (len(needle) == 0):
            return 0
        i = 0
        j = 0
        for i in range(len(haystack)-len(needle)+1):
            for j in range(len(needle)):
                if haystack[i+j] != needle[j]:
            if j == len(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.