查找给定字符串s中最长子字符串的长度,反之亦然。 例如,如果输入是“我喜欢速度快的赛车”,则子串(赛车)长度为7。 如果输入字符串的长度为0,则返回值必须为0.
示例:
"a" -> 1
"aab" -> 2
"abcde" -> 1
"zzbaabcd" -> 4
"" -> 0
需要工作的测试用例:
Test.assertEquals(longestPalindrome("a"), 1)
Test.assertEquals(longestPalindrome("aa"), 2)
Test.assertEquals(longestPalindrome("baa"),3)
Test.assertEquals(longestPalindrome("aab"), 3)
Test.assertNotEquals(longestPalindrome("zyabyz"), 6, "Are you sure that is a palindrome?")
Test.assertEquals(longestPalindrome("baabcd"), 4)
Test.assertEquals(longestPalindrome("baablkj12345432133d"), 9)
到目前为止我的代码:
longestPalindrome=function(s){
var res = s.split(" ");
var sortArr = res.sort(function(a,b){return b.length - a.length});
var res = (sortArr[0]);
var ress = res.length;
return ress;
}
为什么这不合适?