项目作者: page-source

项目描述 :
Find all palindromes in a given string with minimum length of a palindrome to be given by User.
高级语言: JavaScript
项目地址: git://github.com/page-source/find-all-palindromes-in-a-string.git
创建时间: 2018-09-07T12:18:37Z
项目社区:https://github.com/page-source/find-all-palindromes-in-a-string

开源协议:MIT License

下载


Find all palindromes in a string.

All palindromes in a string with minimum length of a palindrome to be given by User.

This repository deals with finding all palindromes in a string. The minimum length of desired palindromes can be set by user. If
user doesn’t set any length ,the default minimum length of palindromes will be set to 3.

Examples - Let’s say our function name is findPalindromes which accepts two parameters - one string & desired minimum length of palindromes.

  • findPalindromes(‘level’ , 5); //outputs level
  • findPalindromes(‘level’ , 4); //outputs level
  • findPalindromes(‘level’ , 3); //outputs level , eve
  • findPalindromes(‘level’ , 2); //outputs level , eve

You get the idea probably!

Here’s my code for finding all palindromes in a string-

  1. const findPalindromes = (str = '', minLengthOfPalindrome = 3) => {
  2. for(let i = 0; i < str.length - minLengthOfPalindrome + 1 ; i++) {
  3. let subStr = '';
  4. for (let j = i; j < str.length - minLengthOfPalindrome + 1; j++) {
  5. subStr = str.substring(i , j + minLengthOfPalindrome);
  6. isPalindrome(subStr);
  7. }
  8. }
  9. }
  10. const isPalindrome = subStr => {
  11. var subPalin = subStr.split('').reverse().join('');
  12. if(subStr === subPalin) {
  13. console.log(subStr);
  14. }
  15. }

Happy Coding!