Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
let result = '';
let start;
let end;
outer:
for (let mid = 0; mid < s.length; mid += 0.5) {
for (let i = Math.floor(mid), j = Math.round(mid); i >= 0, j <= 2 * mid; i--, j++) {
if (s[i] !== s[j]) {
break;
}
start = i;
end = j;
}
let len = end - start + 1;
if (len > result.length) {
result = s.substring(start, end + 1);
}
}
return result;
};
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
const size = s.length;
if (size < 2) {
return s;
}
let start = 0;
let end = 0;
let i;
let j;
const checkPalindrome = function(mid1, mid2) {
i = mid1;
j = mid2;
for (; i >= 0, j < size; i--, j++) {
if (s[i] !== s[j]) {
break;
}
}
if ((j - i - 1) > (end - start)) {
start = i + 1;
end = j - 1;
}
}
for (let mid = 0; mid < size - 1; mid++) {
checkPalindrome(mid, mid);
if (s[mid] === s[mid + 1]) {
checkPalindrome(mid, mid + 1);
}
}
return s.substring(start, end + 1);
};