Find Beautiful Indices in the Given Array I - KMP Algorithm [JS]

Description 

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 from a.
  • Move up the pointer j (for indices in b) while greater than k distance before i.

n = length of sm = 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

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]