Task #5 can be solved in a simpler way
Andrey123815 opened this issue · 0 comments
Andrey123815 commented
var longestPalindrome = function(s) {
let start = 0, end = 0, len = s.length;
for (let i = 0 ; i < len; ++i) {
let maxAvailableLength = maxPalindrLenAroundCenter(s, i, i);
if (maxAvailableLength > end - start + 1) {
start = i - Math.floor(maxAvailableLength / 2);
end = start + maxAvailableLength - 2;
}
}
return s.slice(start, end + 1);
};
var maxPalindrLenAroundCenter = function (s, l, r) {
while(l > 0 && r < s.length - 1 && s[l] === s[r]) {
l--;
r++;
}
return r - l;
};