Skip to main content
edited tags
Link
pacmaninbw
  • 24.8k
  • 11
  • 46
  • 109
Source Link

Optimizing code solution for Palindrome Index-Hackerrank

I submitted my solution for palindrome Index coding challenge but I get "test cases terminated due to time out error". My code is working and so I don't know what else to do to optimize it. Please help:

function palindromeIndex(s) {
    let palindrome = s === s.split('').reverse().join('')
    if(!palindrome) {
        let i = 0
        let integerIndex = []
        let arr = s.split('')
        while(i < s.length) {
            arr.splice(i,1)
            if(arr.join('') === arr.reverse().join('')) {
               integerIndex.push(i) 
            }
            arr = s.split('')
            i++
        }
        return integerIndex.length > 0 ? integerIndex[0] : - 1
    }else {
        return -1
    }
}