28. Implement strStr() leetcode Python new season 2016

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Show Company Tags
Show Tags
Show Similar Problems









python string is immutable .. have to do sth not optimized. may change it in the future.
class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if haystack == "" and needle == "":
            return 0
        if haystack == "" and needle != "":
            return -1
        if needle == "":
            return 0
        newstr = haystack.split(needle)
        if len(newstr[0]) == len(haystack):
            return -1
        return len(newstr[0])


你可能感兴趣的:(28. Implement strStr() leetcode Python new season 2016)