【leetcode❤python】 28. Implement strStr()
#-*- coding: UTF-8 -*-
#题意:大海捞刀,在长字符串中找出短字符串
#AC源码:滑动窗口双指针的方法
class Solution(object):
def strStr(self, hayStack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if len(needle)>len(hayStack):return -1
lenN=len(needle)
needleDic=[];hayStackDic=[]
for i in xrange(len(needle)):
needleDic.append(needle[i])
for i in xrange(0,len(needle)):
hayStackDic.append(hayStack[i])
i=0
while 1:
if needleDic==hayStackDic:
return i
del hayStackDic[0]
if i+lenN>len(hayStack):break
hayStackDic.append(hayStack[i+lenN])
i+=1
return -1
sol=Solution()
print sol.strStr("pi","pi")