Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
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])