[prev] 23 [next]

Boyer-Moore Algorithm (cont)

BoyerMooreMatch(T,P,Σ):
|  Input  text T of length n, pattern P of length m, alphabet Σ
|  Output starting index of a substring of T equal to P
|         -1 if no such substring exists
|
|  L=lastOccurenceFunction(P,Σ)
|  i=m-1, j=m-1                 // start at end of pattern
|  repeat
|  |  if T[i]=P[j] then
|  |  |  if j=0 then
|  |  |     return i            // match found at i
|  |  |  else
|  |  |     i=i-1, j=j-1        // keep comparing
|  |  |  end if
|  |  else                      // character-jump
|  |  |  i=i+m-min(j,1+L[T[i]])
|  |  |  j=m-1
|  |  end if
|  until i≥n
|  return -1                    // no match

  • Biggest jump (m characters ahead) occurs when L[T[i]] = -1