Created
March 14, 2022 14:28
-
-
Save kevinmmartins/a24cf9a31bc3c1dd89b862f81806cdfc to your computer and use it in GitHub Desktop.
Algorithm to find the largest palindrome in a string
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 SearchingChallenge(str) { | |
let arrayString = str.split("") | |
let reversedArrayString = str.split("").reverse() | |
let reversedString = reversedArrayString.join("") | |
let result = '' | |
let hasNext = true | |
let i = 0 | |
while(hasNext){ | |
for(let j = i + 1 ; j<= arrayString.length ; j ++){ | |
const slicedString = arrayString.slice(i,j).join("") | |
if(slicedString && slicedString.length>2 && reversedString.includes(slicedString)){ | |
if(slicedString.length > result.length) result = slicedString | |
} | |
} | |
i = i+1 | |
if(i >= arrayString.length){ | |
hasNext = false | |
} | |
} | |
return result.length > 2 ? result : 'none' | |
} | |
// keep this function call here | |
console.log(SearchingChallenge(readline())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment