给你一个字符串 s
,找到 s
中最长的 回文子串。
示例 1:
输入:s = "babad" 输出:"bab" 解释:"aba" 同样是符合题意的答案。
示例 2:
输入:s = "cbbd" 输出:"bb"
/*** @param {string} s* @return {string}*/
var longestPalindrome = function (s) {if (s.length == 1) {return s;}let dp = [];for (let i = 0; i < s.length; i++) {dp[i] = new Array();for (let j = 0; j < s.length; j++) {dp[i][j] = 0;}}for (let i = 0; i < s.length; i++) {dp[i][i] = 1;}let start = 0;let max_len = 1;for (let len = 2; len <= s.length; len++) {for (let i = 0; i < s.length; i++) {let j = len + i - 1if (s[i]!=s[j]) {dp[i][j] = 0} else {if (len==2) {dp[i][j] = 1}else{dp[i][j] = dp[i+1][j-1]}}if (dp[i][j]==1&&j-i+1>max_len) {start = imax_len = j-i+1}}}return s.substring(start, start + max_len);
};
let str = "ccc";
console.log(longestPalindrome(str));