3. What will be the output of the following JavaScript code snippet using KMP to find pattern 'ABABC' in text 'ABABABCABABCBC'?
function KMPSearch(pattern, text) {
const lps = computeLPSArray(pattern);
let i = 0, j = 0;
const result = [];
while (i < text.length) {
if (pattern[j] === text[i]) {
i++; j++;
if (j === pattern.length) {
result.push(i - j);
j = lps[j - 1];
}
} else if (j !== 0) {
j = lps[j - 1];
} else {
i++;
}
}
return result;
}
function computeLPSArray(pattern) {
const lps = new Array(pattern.length).fill(0);
let length = 0, i = 1;
while (i < pattern.length) {
if (pattern[i] === pattern[length]) {
length++;
lps[i] = length;
i++;
} else if (length !== 0) {
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
return lps;
}
console.log(KMPSearch('ABABC', 'ABABABCABABCBC'));