/** * @param {string} s * @return {string} */ var longestPalindrome = function (s) { let start = 0, end = 0; let maxLen = 0; for (let i = 0; i < s.length; i++) { centerSpread(i, i, s); centerSpread(i, i + 1, s); } return s.slice(start, end + 1);

functioncenterSpread(center1, center2, s) { while (center1 >= 0 && center2 < s.length && s[center1] === s[center2]) { // If statement must proceed changes to center1 and center2. Because new boundaries s[center1]. s[center2] might not be equal. if (center2 - center1 + 1 > maxLen) { start = center1; end = center2; maxLen = center2 - center1 + 1; } center1--; center2++; } } };

var longestPalindrome = function (s) { let result = ''; for (let i = 0; i < s.length; i++) { for (let j = 0; j <= i; j++) { const str = s.slice(j, i + 1); if (isPalindrome(str)) { result = i - j + 1 > result.length ? str : result; } } } return result; };

functionisPalindrome(str) { let left = 0, right = str.length - 1; while (left <= right) { if (str[left] !== str[right]) returnfalse; left++; right--; } returntrue; }