Advertisement
Iam_Sandeep

Boyer Moore ALgorithm

Jun 20th, 2022 (edited)
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. class Solution:
  2.     def strStr(self, text: str, pat: str) -> int:
  3.         item=[-1]*256
  4.         if not pat:return 0
  5.         #creating bad character array . preprocessing pattern
  6.         for i,j in enumerate(pat):
  7.             item[ord(j)]=i
  8.         m,n=len(text),len(pat)
  9.         i=0#used for text
  10.         while i<=m-n:# if it crosses m-n then you wont get sufficient length of pattern to search
  11.             j=n-1#used for pattern
  12.             while j>=0 and text[i+j]==pat[j]:
  13.                 j=j-1
  14.             if j<0:
  15.                 return i
  16.             else:
  17.                 i=i+max(1,j-item[ord(text[i+j])]) #j-item[ord(text[i+j])] might be zero some times
  18.         return -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement