Implement strStr()
10 Aug 2018给定两个字符串strA
/ strB
,计算后者在前者第一次出现的首字母位置(java
的indexOf()
和C
的strStr()
)
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle:
return 0
if haystack == needle:
return 0
for i in range(len(haystack)):
subStr = ''.join(haystack[i : i + len(needle)])
if len(needle) > len(subStr): #在输入的两个字符很长的情况下,有可能存在资源消耗过大,因此先用长度做简单判断
return -1
if subStr == needle:
return i
return -1