Longest Palindromic Substring
Given a strings, find the longest palindromic substring ins. You may assume that the maximum length ofsis 1000.
Example 1:
*Input:* “babad”
*Output:* “bab”
*Note:* “aba” is also a valid answer.
Example 2:
*Input:* “cbbd”
*Output:* “bb”
solution:
function longestPalinDrome(s) {
function expandAroundCneter(left, right) {
while (left >= 0 && right < s.length && s[left] === s[right]) {
left--;
right++;
}
return s.slice(left + 1, right);
}
if (!s) {
return "";
}
let longest = s[0];
for (var i = 0; i < s.length; i++) {
let odd = expandAroundCneter(i, i);
if (odd.length > longest.length) {
longest = odd;
}
let even = expandAroundCneter(i, i + 1);
if (longest.length < even.length) {
longest = even;
}
}
return longest;
}
网友评论