获取字符串中最长的对称的字串的大小

longestPalindrome=function(s){ 

if(s.length==1) return 1; 

var a=0; for(vari=0;i

for(var j=i+1;j<=s.length;j++) {  

if(s.slice(i,j).split("").join("")== (s.slice(i,j)).split("").reverse().join("")) {  

a=(j-i)>a?(j-i):a;  

}

}

return a;

}



`Test.assertEquals(longestPalindrome("a"), 1)Test.assertEquals(longestPalindrome("aa"), 2)Test.assertEquals(longestPalindrome("baa"), 2)Test.assertEquals(longestPalindrome("aab"), 2)Test.assertEquals(longestPalindrome("baabcd"), 4)Test.assertEquals(longestPalindrome("baablkj12345432133d"), 9)

你可能感兴趣的:(js)