我的代码在控制台中运行,但在hackerank中给出了错误的结果 问题:从字符串中打印出所有属于回文的子字符串的计数
function isPalindrome(str) {
var len = str.length;
var mid = Math.floor(len / 2);
for (var i = 0; i < mid; i++) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
//Had to use this lengthy function because in
//str == str.split('').reverse().join('');
//I was getting error that split is not a function
return true;
}
function scatterPalindrome(str) {
var result = [],
c = 0;
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length + 1; j++) {
result.push(str.slice(i, j));
}
}
for (let i = 0; i < result.length; i++) {
let k = result[i];
if (isPalindrome(k))
c++;
}
return c; // the answer was always 1
}
console.log(scatterPalindrome("abc"));
输入:“ abc”
预期输出:3
实际输出:1
答案 0 :(得分:0)
由于我无法发表评论,因此在这里回答,我会说,您应该检查他们是否提到了很多测试用例,并且在每个测试用例中都必须进行查询,这样可以合理地确定您的输出及其输出不匹配
take no.of testcases input
while(testcases counter doesn't reach 0 )
take string input
call your function for answer for input and print
decrement testcases counter