Saltar a contenido

28. Find the Index of the First Occurrence in a String

28
Find the Index of the First Occurrence in a String Easy
Tags: String, String Matching, Two Pointers

Solución (Python)

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

sol = Solution()
print(sol.strStr("SADBUTSAD", "SAD"))
print(sol.strStr("leetcode", "leeto"))