Python brutal force with only basic functions


  • 0
    W
    class Solution(object):
        def strStr(self, haystack, needle):
            """
            :type haystack: str
            :type needle: str
            :rtype: int
            """
            if not haystack and not needle:
                return 0
            i = 0
            start = 0
            while i < len(haystack):
                j = 0
                while j < len(needle):
                    if haystack[i] == needle[j]:
                        i +=1
                        j +=1
                        if i == len(haystack) and j < len(needle):
                            return -1
                    else:
                        start += 1
                        break
                if j == len(needle):
                    return start
                else:
                    i = start
            return -1

Log in to reply
 

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