Skip to content

Instantly share code, notes, and snippets.

@neerajsingh0101
Forked from ajith-k-v/longestPalindrome.js
Created November 9, 2021 10:34
Show Gist options
  • Save neerajsingh0101/3c6192bd82cec0bf2c04f9b57a5b3e11 to your computer and use it in GitHub Desktop.
Save neerajsingh0101/3c6192bd82cec0bf2c04f9b57a5b3e11 to your computer and use it in GitHub Desktop.
JavaScript task: Find longest palindrome of the string "hellonevenlikestacocatandkayak"
function is_Palindrome(str1) {
var rev = str1.split("").reverse().join("");
return str1 == rev;
}
function longest_palindrome(str1){
var max_length = 0,
maxp = '';
for(var i=0; i < str1.length; i++)
{
var subs = str1.substr(i, str1.length);
for(var j=subs.length; j>=0; j--)
{
var sub_subs_str = subs.substr(0, j);
if (sub_subs_str.length <= 1)
continue;
if (is_Palindrome(sub_subs_str))
{
if (sub_subs_str.length > max_length)
{
max_length = sub_subs_str.length;
maxp = sub_subs_str;
}
}
}
}
return maxp;
}
console.log(longest_palindrome("hellonevenlikestacocatandkayak"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment