Palindrome Check

On August 28, 2015, in Code, Javascript, Work - out of office, by sailesh

A palindrome is a string when reversed is the same as original.

Example:

  • String Input:
  • Is Input a Palindrome?:
  • All Distinct Palindrome Substrings:
  • No. of Distinct Palindrome Substrings:

Javascript code:

    function findAllDistinctPalindromes(str) {
        //if(str.length>5000)return "String too big";
        var strarr = [];
        for (var lenOfSubStr = 1; lenOfSubStr <= str.length; lenOfSubStr++) {
            for (var i = 0;
                (i + lenOfSubStr) <= str.length; i++) {
                var subString = str.substr(i, lenOfSubStr);
                if(strarr.indexOf(subString) < 0) {
                if(isPalindrome(subString))strarr.push(subString);
            }
            }
        }
        return strarr;
    }

    function allDistinctPalindromesCount(str) {
        return findAllDistinctPalindromes(str).length || 0;
    }

    function isPalindrome(inputStr) {
        return (inputStr == inputStr.split('').reverse().join(''));
    }


 

Leave a Reply

Enable Google Transliteration.(To type in English, press Ctrl+g)