Python solution


  • 0
    H
    class Solution:
        # @param {string} haystack
        # @param {string} needle
        # @return {integer}
        def strStr(self, haystack, needle):
            if len(haystack)<len(needle):
                return -1
            for i in range(len(haystack)-len(needle)+1):
                found=True
                for j in range(len(needle)):
                    if haystack[i+j]!=needle[j]:
                        found=False
                        break
                if found:
                    return i
            return -1

  • 2
    S

    You can replace the found flag with a for/else clause.


Log in to reply
 

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