It seems that the problem is in this line:
while (x < alen && y < blen && a[x] == b[x]) {
Here b[x] should be b[y] , which gives:
while (x < alen && y < blen && a[x] == b[y]) {
With this change, the results for your examples are 6, 0, 2, and 1. This seems accurate.
source share