Simple Python solution (38 ms)


  • 0
    B
    class Solution(object):
        def strStr(self, haystack, needle):
            """
            :type haystack: str
            :type needle: str
            :rtype: int
            """
            # check if needle in haystack
            if needle in haystack: 
                if len(needle) == 0:
                    return 0
                else:
                    for i in xrange(len(haystack)):
                            if needle == \
                              haystack[i:i+len(needle)]:
                                return i
            else:
                return -1
                    
    

Log in to reply
 

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