Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def strStr(self, text: str, pat: str) -> int:
- item=[-1]*256
- if not pat:return 0
- #creating bad character array . preprocessing pattern
- for i,j in enumerate(pat):
- item[ord(j)]=i
- m,n=len(text),len(pat)
- i=0#used for text
- while i<=m-n:# if it crosses m-n then you wont get sufficient length of pattern to search
- j=n-1#used for pattern
- while j>=0 and text[i+j]==pat[j]:
- j=j-1
- if j<0:
- return i
- else:
- i=i+max(1,j-item[ord(text[i+j])]) #j-item[ord(text[i+j])] might be zero some times
- return -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement