Created
June 9, 2020 01:13
-
-
Save JoeCortopassi/db5d279b9d21b71d7bdbd28a41f109ba to your computer and use it in GitHub Desktop.
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 biggestPalindrome (str) { | |
if (str.length === 1) return str; | |
const pieces = str.split(''); | |
let longestPalindrome = ''; | |
for (let i = 1; i <= pieces.length; i++) { | |
let offset = 1; | |
do { | |
if (pieces[i-offset] === pieces[i+offset]) { | |
const newPalindrome = str.substring(i-offset, i+offset+1); | |
if (newPalindrome.length > longestPalindrome.length) longestPalindrome = newPalindrome; | |
offset++; | |
continue | |
} | |
if (pieces[i-offset] === pieces[i+(offset-1)]) { | |
const newPalindrome = str.substring(i-offset, i+(offset)); | |
if (newPalindrome.length > longestPalindrome.length) longestPalindrome = newPalindrome; | |
offset++; | |
continue | |
} | |
break; | |
} while (offset < 100); | |
} | |
return longestPalindrome; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment