Question: Give a justification of why the computeFailKMP method (Code Fragment 13.4) runs in O(m) time on a pattern of length m. 1 private static int[]

Give a justification of why the computeFailKMP method (Code Fragment 13.4) runs in O(m) time on a pattern of length m.

1 private static int[] computeFailKMP(char[ ] pattern) { int m = pattern.length; int[ ] fail = new int[m]; int j = 1; 2

1 private static int[] computeFailKMP(char[ ] pattern) { int m = pattern.length; int[ ] fail = new int[m]; int j = 1; 2 // by default, all overlaps are zero 3 4 int k = 0; while (j < m) { if (pattern[i] = faili] = k + 1; j++; k++; } else if (k > 0) k = fail[k-1]: else 5 // compute fail li] during this pass, if nonzero // k +1 characters match thus far pattern[k]) { 10 // k follows a matching prefix 11 12 // no match found starting at j 13 14 j++; } return fail; 15 16 17 }

Step by Step Solution

3.34 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The justification is similar to the argument that the number of i... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction to Algorithms Questions!