3. What will be the output of the following TypeScript code snippet using Rabin Karp to find pattern
"abc" in text
"ababcabc"?
const text = "ababcabc";
const pattern = "abc";
const d = 256;
const q = 101;
function rabinKarp(text: string, pattern: string): number[] {
const m = pattern.length;
const n = text.length;
let p = 0, t = 0, h = 1;
const result: number[] = [];
for (let i = 0; i < m - 1; i++) h = (h * d) % q;
for (let i = 0; i < m; i++) {
p = (d * p + pattern.charCodeAt(i)) % q;
t = (d * t + text.charCodeAt(i)) % q;
}
for (let i = 0; i <= n - m; i++) {
if (p === t) {
let match = true;
for (let j = 0; j < m; j++) {
if (text[i + j] !== pattern[j]) {
match = false;
break;
}
}
if (match) result.push(i);
}
if (i < n - m) {
t = (d * (t - text.charCodeAt(i) * h) + text.charCodeAt(i + m)) % q;
if (t < 0) t += q;
}
}
return result;
}
console.log(rabinKarp(text, pattern));