Last active
November 9, 2021 10:34
-
-
Save ajith-k-v/a4314af3fe7f176cdab46ce7b393b0cd to your computer and use it in GitHub Desktop.
JavaScript task: Find longest palindrome of the string "hellonevenlikestacocatandkayak"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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