Skip to content

Instantly share code, notes, and snippets.

@kiliczsh
Last active February 15, 2022 19:02
Show Gist options
  • Select an option

  • Save kiliczsh/5dbf16a84dc4946c107272a02db228dd to your computer and use it in GitHub Desktop.

Select an option

Save kiliczsh/5dbf16a84dc4946c107272a02db228dd to your computer and use it in GitHub Desktop.
var longestPalindrome = function(str) {
let strLen = str.length;
let maxLen = 0, leftResult = 0, rightResult = 0, index = 0;
while(index < strLen){
let left = index-1;
while(left >= 0 && str[index] == str[left]){
left--;
}
let right = index+1;
while(right < strLen && str[index] == str[right]){
right++;
}
let end = right;
while(left >= 0 && right < strLen && str[left] == str[right]){
left--;
right++;
}
if(maxLen < right-left-1){
maxLen = right-left-1
leftResult = left+1;
rightResult = right-1;
}
index = end
}
return str.substring(leftResult,rightResult+1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment