Created
March 21, 2022 22:34
-
-
Save see-why/faabc32de8c59084168ad29d2fa26441 to your computer and use it in GitHub Desktop.
Hackerrank Palindrome Index challenge solution
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
// https://www.hackerrank.com/challenges/palindrome-index/problem?isFullScreen=true | |
function palindromeIndex(s) { | |
// Write your code here | |
function isPalendrome(s, index){ | |
let result = false; | |
let subString = s.substring(0, index) + s.substring(index+1); | |
let reverseString = subString.split('').reverse().join(''); | |
if(subString == reverseString){ | |
result = true; | |
} | |
return result; | |
} | |
let index = -1 | |
if(s !== s.split('').reverse().join('')){ | |
for(let i=0; i<s.length/2; i++){ | |
let letter = s[i]; | |
let alternate_index = s.length - (i+1); | |
let alternate_letter = s[alternate_index]; | |
if(letter != alternate_letter){ | |
let subString = s.substring(0, index) + s.substring(index+1); | |
if(isPalendrome(s, i)){ | |
index = i; | |
} else if(isPalendrome(s, alternate_index)){ | |
index = alternate_index; | |
} | |
break; | |
} | |
} | |
} | |
return index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment