Five-line Python solution.


  • 0

    Here's my five-line Python solution:

        def strStr(self, haystack, needle):
            if haystack == needle or needle == "": return 0
            for i in range(len(haystack) - len(needle) + 1):
                if haystack[i:i+len(needle)] == needle: return i
            return -1
    

    It's clean and simple, with no excess space used.


Log in to reply
 

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