Skip to content

Instantly share code, notes, and snippets.

@cicconewk
Created May 9, 2019 15:58
Show Gist options
  • Save cicconewk/035f5b55c4477c7b47a3c27e85d5bd83 to your computer and use it in GitHub Desktop.
Save cicconewk/035f5b55c4477c7b47a3c27e85d5bd83 to your computer and use it in GitHub Desktop.
Convert to palindrome
function reverseString (str) {
return str.split('').reverse().join('')
}
function palindromeIndex (palindrome_str) {
if (reverseString(palindrome_str) === palindrome_str) {
return -2
}
const strlen = palindrome_str.length
for (let i = 0; i < strlen; i++) {
const partial_str = palindrome_str.substring(0,i) + palindrome_str.substring(i+1,strlen)
if (partial_str === reverseString(partial_str)) {
return i
}
}
return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment