Skip to content

Latest commit

 

History

History
30 lines (26 loc) · 630 Bytes

028.md

File metadata and controls

30 lines (26 loc) · 630 Bytes

28. Implement strStr()

Solution 1

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

Solution 2

class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        return haystack.find(needle)