educative.io

Check my algorithm to check whether the string contains a palindrome of the given pattern

const find_permutation = function(str, pattern) {
  let patternCounter={},windowStart=0;
  for(let char of pattern){
    patternCounter[char]=patternCounter[char] ? patternCounter[char]+1 : 1;
  }
  for(let windowEnd=0;windowEnd<str.length;windowEnd++){
    let rightChar=str[windowEnd];
    if(!(rightChar in patternCounter)){
      windowStart=windowEnd+1;
    }
    if(windowEnd-windowStart+1===pattern.length) return true;
  }
  return false;
};

This seems to pass all the 4 test cases, please do check and let me know if this will fail any test cases, the provided solution looks more complex than this.