Counting Palindromes using recursion

三世轮回 提交于 2021-01-05 12:47:26

问题


I currently have a code which counts the palindromes in a given string and it was working fine till I tested it with "appal" the function returned 0 when it should return 2 (appa and pp) I would really appreciate it if someone can edit my current code so that it meets that requirement, thank you! Here's my code:

function countPalindromes(string, count) {
  if (string.length <= 1) {
    return count;
  }

  let [ firstLetter ] = string;
  let lastLetter = string[string.length - 1];

  if (firstLetter === lastLetter) {
    let stringWithoutFirstAndLastLetters = string.substring(1, string.length - 1);
    return countPalindromes(stringWithoutFirstAndLastLetters, count + 1);
  } else {
    return 0;
  }
}

console.log(countPalindromes("kayak", 0));
console.log(countPalindromes("aya", 0));
console.log(countPalindromes("appal", 0));

回答1:


I think this function does the trick. Happy to refactor and explain it later. I'd rather write a new function because I don't think your code is close to executing the task.

function returnNumberOfPalindromes(word) {
    function isPalindrome(chunk) { 
        return [...chunk].reverse().join('') === chunk;
    }
    let tally = 0;
  
    for (let index1 = 0; index1 <= word.length; index1++) {
      for (index2 = index1 + 2; index2 <= word.length; index2++) { 
        let chunk = word.slice(index1, index2); 
        if (isPalindrome(chunk)) {
            tally += 1;
        };
      }
    }
    console.log(tally);
}

returnNumberOfPalindromes("kayak");
returnNumberOfPalindromes("aya");
returnNumberOfPalindromes("appal");
returnNumberOfPalindromes("addadaadd");



回答2:


function isPalindrome(str) {
  return str == str.split("").reverse().join("");
}
//iterative only solution
function countPalindromes(s) {
  let count = 0;
  for (let i = 0; i < s.length - 1; i++) {
    const sub = s.slice(i);

    for (let j = 2; j < sub.length + 1; j++) {
      if (isPalindrome(sub.slice(0, j))) {
        count++
      }
    }
  }
  return count;
}

console.log(countPalindromes("kayak"));
console.log(countPalindromes("aya"));
console.log(countPalindromes("appal"));

You are comparing the first letter with the last letter and will return zero because this will be false for appal yet true for the other 2 test cases.




回答3:


  1. So basically if you find a palindrome like pp or apa by scanning the string and comparing the current with current+1 and current+2 in an iterative loop and storing the matches as objects with start end end index.

  2. You initiate a count which is the length of the array.

  3. Update the array by filter the elements where you update the object with one less and one more for start end if the 1 larger is a palindrome and return true or return false and get it removed.

  4. if the array is length zero you return the count

  5. add the array length to the count and repeat from 3.



来源:https://stackoverflow.com/questions/64796628/counting-palindromes-using-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!