String Algorithms
Print this page

Algorithm for Failure function

FailureFunction(P)

  1. m= stringlength(P)
  2. FF[1] 0
  3. k 0
  4. for j 2 to m
  5. while k>0 and P[k+1] P[j]
  6. do k FF [k]
  7. if P[k+1]=P[j]
  8. then k = k+1
  9. FF[j] k
  10. return FF
         
     

       
Fig : 5 : failure function for the above pattern
 

The running time of FailureFunction is O(m) where m is the length of the pattern P.

Prev