DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Naive Pattern Search Method

const naiveSearchPatter = (searchPattern, string) => {
  const M = searchPattern.length;
  const N = string.length;

  const indexArray = [];
  for (let i = 0; i <= N-M; i++) {
    let j;
    for (j = 0; j < M; j++) {
      if (string[i+j] != searchPattern[j]) 
      break;
    }
      if (j == M) {
          indexArray.push(i)
      }
  }
  return indexArray;
};

console.log(naiveSearchPatter("bc", "abcd"));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)