https://leetcode.com/problems/longest-palindromic-substring/
Given a string S, find the longest palindromic substring in S. You may
assume that the maximum length of S is 1000, and there exists one
unique longest palindromic substring.
输入字符串 S, 求字符串中最长的回文子字符串,并返回,如输入 dbakokabbbbbbb 返回 bakokab
var longestPalindrdome = function (s) {
var t = s.split("").join("#");
var c = 1, e = 0, cs = 0;
t = "~" + t + "#";
for (var j = 1, lenj = t.length - 1; j < lenj; j++, c = 1) {
while (t[j + c] === t[j - c]){
c++;
}
if (c > e) {
e = c;
cs = j;
}
}
var result = t.slice(cs - e + 1, cs + e).replace(/#/g, "").replace(/~/g, "");
return result;
};
求解更快的算法,不知道 200ms 的算法是怎么样的?
测试用例之一:
var s = 'whdqcudjpisufnrtsyupwtnnbsvfptrcgvobbjglmpynebblpigaflpbezjvjgbmofejyjssdhbgghgrhzuplbeptpaecfdanhlylgusptlgobkqnulxvnwuzwauewcplnvcwowmbxxnhsdmgxtvbfgnuqdpxennqglgmspbagvmjcmzmbsuacxlqfxjggrwsnbblnnwisvmpwwhomyjylbtedzrptejjsaiqzprnadkjxeqfdpkddmbzokkegtypxaafodjdwirynzurzkjzrkufsokhcdkajwmqvhcbzcnysrbsfxhfvtodqabvbuosxtonbpmgoemcgkudandrioncjigbyizekiakmrfjvezuzddjxqyevyenuebfwugqelxwpirsoyixowcmtgosuggrkdciehktojageynqkazsqxraimeopcsjxcdtzhlbvtlvzytgblwkmbfwmggrkpioeofkrmfdgfwknrbaimhefpzckrzwdvddhdqujffwvtvfyjlimkljrsnnhudyejcrtrwvtsbkxaplchgbikscfcbhovlepdojmqybzhbiionyjxqsmquehkhzdiawfxunguhqhkxqdiiwsbuhosebxrpcstpklukjcsnnzpbylzaoyrmyjatuovmaqiwfdfwyhugbeehdzeozdrvcvghekusiahfxhlzclhbegdnvkzeoafodnqbtanfwixjzirnoaiqamjgkcapeopbzbgtxsjhqurbpbuduqjziznblrhxbydxsmtjdfeepntijqpkuwmqezkhnkwbvwgnkxmkyhlbfuwaslmjzlhocsgtoujabbexvxweigplmlewumcone';
// 返回:wfdfw
原始代码帮你注释一下
用 Manacher 算法来动态规划算出最大回文子串。
也不知道我理解得对不对,英文的应该翻译成中文的,代码应该直接贴出来的。
运行结果:93ms
答案莫名其妙被踩,已刪除。