Find Beautiful Indices in the Given Array I - KMP Algorithm [JS]
Solution: KMP Algorithm & Two Pointers
Use a modified KMP algorithm to find all matches of a
in s
, and the same for b
.
Use two pointers to find the number of indices from a
and b
that are at most k
distance apart.
- Anchor the pointer
i
going through indices froma
. - Move up the pointer
j
(for indices inb
) while greater thank
distance beforei
.
n = length of s
, m = length of a and b
Time Complexity: O(n + m)
Space Complexity: O(m)
var beautifulIndices = function(s, a, b, k) {
let aIndices = kmp(s, a), bIndices = kmp(s, b);
let ans = [];
for (let i = 0, j = 0; i < aIndices.length && j < bIndices.length; i++) {
while (j < bIndices.length && aIndices[i] - bIndices[j] > k) j++;
if (j < bIndices.length && Math.abs(bIndices[j] - aIndices[i]) <= k) ans.push(aIndices[i]);
}
return ans;
};
function kmp(str, substr) {
let lps = getLPS(substr);
let n = str.length, m = substr.length;
let i = 0, j = 0;
let matches = [];
while (j < n) {
if (str[j] === substr[i]) {
i++, j++;
if (i === m) matches.push(j - m);
} else if (i > 0) {
i = lps[i - 1]; // rollback
} else j++; // i is 0, so we move j forward
}
return matches;
}
function getLPS(str) {
let n = str.length, lps = Array(n).fill(0);
let i = 0, j = 1;
while (j < n) {
if (str[i] === str[j]) {
lps[j++] = ++i;
} else if (i > 0) {
i = lps[i - 1];
} else j++;
}
return lps;
}
Comments
Post a Comment