function kmpGetStrPartMatchValue(str) {
var prefix = [];
var suffix = [];
var partMatch = [];
for(var i =0,j=str.length;i
var newStr = str.substring(0,i 1);
if(newStr.length == 1){
partMatch[ i] = 0;
} else {
[k] = newStr.slice(-k-1);
if(prefix[k] == suffix [k]){
partMatch[i] = prefix[k].length;
}
}
if(!partMatch[i]){
partMatch[i] = 0;
}
}
}
prefix.length = 0;
suffix.length = 0;
return partMatch;
}
//デモvar t="ABCDABD";console.log(kmpGetStrPartMatchValue(t));
//output:[0,0,0,0,1,2,0 ]
フォールバック アルゴリズムは次のように実装されます: