Hackerrank: Palindrome Index Solution
1function palindromeIndex(s) {2 function isPalindrome(s) {3 for (let i = 0; i < Math.floor(s.length / 2); i++) {4 if (s[i] !== s[s.length - i - 1]) {5 return false6 }7 }8 return true9 }1011 let res = -11213 for (let i = 0; i < Math.floor(s.length / 2); i++) {14 if (s[i] !== s[s.length - i - 1]) {15 const tmp = s.substring(0, i) + s.substring(i + 1)16 if (isPalindrome(tmp)) {17 return i18 } else {19 return s.length - i - 120 }21 }22 }2324 return -125}
Comments
Loading comments...
Tags
hackerrank
string
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.