DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Longest Palindrome

var longestPalindrome = function(s) {

  let trackkerObject = new Map();
  let n = 0;
  for (let i = 0; i < s.length; i++) {
    if (trackkerObject.has(s[i])) {
      n += 2;
      trackkerObject.delete(s[i]);
    } else {
      trackkerObject.set(s[i], i);
    }
  }

  return trackkerObject.size > 0 ? n+1 : n;
};

console.log(longestPalindrome("abccccdd"));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)