You are viewing a single comment's thread. Return to all comments →
javascript
function palindromeIndex(s) { const isPalindrome = (str) => { let i = 0, j = str.length - 1 while (i < j) { if (str[i] != str[j]) break i++; j-- } return { yes: i >= j, left: i, right: j } } const { yes, left, right } = isPalindrome(s) if (yes) return -1 const { yes: yesLeft } = isPalindrome(s.slice(0, left).concat(s.slice(left + 1))) if (yesLeft) return left const { yes: yesRight } = isPalindrome(s.slice(0, right).concat(s.slice(right + 1))) if (yesRight) return right return -1 }
Seems like cookies are disabled on this browser, please enable them to open this website
Palindrome Index
You are viewing a single comment's thread. Return to all comments →
javascript