最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calculate how many sub-strings of a string are palindromes - Stack Overflow

programmeradmin0浏览0评论

I'm currently trying to work out how many substrings of a given string are palindromes.

When given the string aabaa the expected output is 5 however my code outputs 4. I'm not too sure why, can any one help me solve this?

My code:

function countPalindromesInString(s) {
    let count = 0;

    if (s === s.split('').reverse().join('')) {
        count += 1;
    }

    let testStr = '';
    for (let i = 0; i < s.length; i++) {
        testStr += s[i];

        if (testStr === testStr.split('').reverse().join('')) {
            count += 1;
        }
    }
    return count;
}

I'm currently trying to work out how many substrings of a given string are palindromes.

When given the string aabaa the expected output is 5 however my code outputs 4. I'm not too sure why, can any one help me solve this?

My code:

function countPalindromesInString(s) {
    let count = 0;

    if (s === s.split('').reverse().join('')) {
        count += 1;
    }

    let testStr = '';
    for (let i = 0; i < s.length; i++) {
        testStr += s[i];

        if (testStr === testStr.split('').reverse().join('')) {
            count += 1;
        }
    }
    return count;
}
Share Improve this question asked Nov 12, 2017 at 19:54 Matt KentMatt Kent 1,1851 gold badge11 silver badges26 bronze badges 4
  • Print the strings you find, and you should see the problem? – Fuhrmanator Commented Nov 12, 2017 at 19:58
  • If the characters can be bined in any order you first need all permutations. You are only working from left to right – charlietfl Commented Nov 12, 2017 at 20:07
  • @charlietfl I assumed that because I was reversing the string as I iterated through that this wouldn't matter, or is this not the case? – Matt Kent Commented Nov 12, 2017 at 20:27
  • Depends on the constraints of the assignment. – charlietfl Commented Nov 12, 2017 at 20:30
Add a ment  | 

2 Answers 2

Reset to default 5

I'm not sure why you expect output 5 for input aabaa. From my point of view, if you consider a single letter as a palindrome than the output should be 9, otherwise the result should be 4: "aa", "aa", "aba", "aabaa". Your code only count from left to right and also double counts the full 5 letter string, once in the beginning, here:

if (s === s.split('').reverse().join('')) { count += 1; }

and once in the for loop for case i=4;

Here is a solution to your question:

function countPalindromesInString(s) {
    let count = 0;  //or s.length if you chose to count single letters as palindrome
    let subString;

    for (let i = 1; i < s.length; i++) {
      for(let j = 0; j < s.length - i; j++) {
        subString = s.substring(j, j+i+1);
        if(subString === subString.split('').reverse().join('')) {
            count += 1;
        }
      }
    }
    return count;
}

Later Edit:

If we want to count unique palindromes in your string, we can store the palindromes found in an array and every time we find another one, we check if it was previously added:

function countPalindromesInString(s) {
    let subStrings = [];

    for (let i = 0; i < s.length; i++) {
      for(let j = 0; j < s.length - i; j++) {
        let subString = s.substring(j, j+i+1);
        if(subString === subString.split('').reverse().join('') && !subStrings.includes(subString)) {
          subStrings.push(subString);
        }
      }
    }
    return subStrings.length;
}

function soln(inp){
    const op = []
    for(let i=0; i<input.length; i++){
        for(let j=i; j<=input.length; j++){
            let innerInp = input.substring(i,j)
            if(innerInp.length > 1 && (innerInp === innerInp.split('').reverse().join(''))){
                op.push(innerInp);
            }
        }
    }
   return op.length
};

发布评论

评论列表(0)

  1. 暂无评论