Following an example for which it does not work:
i 0 1 2 3 4 5 p ABABBA c1 0 0 1 2 0 1 c2 0 0 1 2 2 3
Cause:
At i=4, len=2 p[i]='B' and p[len]='A' //Mismatch! lps string upto i=3: AB(0-1 prefix), (2-3 suffix) ------------------------------- i=4 Next charecter: B len=2 // longest prefix suffix length Charecter looking for : A (=p[len])
Thus, until i = 3, we used AB (0-1) as the prefix that corresponded to the suffix AB (2-3), but now there is a mismatch in i = 4, so we see that t extends the original prefix (0- 1), so the checked position is the prefix found before "AB", which is executed lps [len-1] -1, since the array starts with 0>, and this is not necessarily len-1, since we may need to step further, to get a new long prefix suffix.
Sahil sareen
source share