Skip to content

Instantly share code, notes, and snippets.

@ajith-k-v
Last active November 9, 2021 10:34
Show Gist options
  • Save ajith-k-v/a4314af3fe7f176cdab46ce7b393b0cd to your computer and use it in GitHub Desktop.
Save ajith-k-v/a4314af3fe7f176cdab46ce7b393b0cd 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