Created
May 9, 2019 15:58
-
-
Save cicconewk/035f5b55c4477c7b47a3c27e85d5bd83 to your computer and use it in GitHub Desktop.
Convert to palindrome
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 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