Number of positions where a letter can be inserted such that a string becomes palindrome | Javascript code to find the no . of positions where a letter can be inserted to make it a palindrome ; Function to check if the string is palindrome ; to know the length of string ; if the given string is a palindrome ( Case - I ) ; Sub - case - III ) ; if ( n % 2 == 0 ) if the length is even ; count = 2 * count + 1 ; sub - case - I ; count = 2 * count + 2 ; sub - case - II ; insertion point ; Case - I ; Case - II ; Driver code
function isPalindrome ( s , i , j ) { let p = j ; for ( let k = i ; k <= p ; k ++ ) { if ( s [ k ] != s [ p ] ) return false ; p -- ; } return true ; } function countWays ( s ) { let n = s . length ; let count = 0 ; if ( isPalindrome ( s , 0 , n - 1 ) ) { for ( let i = n / 2 ; i < n ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) count ++ ; else break ; } { count ++ ; } else } else { for ( let i = 0 ; i < n / 2 ; i ++ ) { if ( s [ i ] != s [ n - 1 - i ] ) { let j = n - 1 - i ; if ( isPalindrome ( s , i , n - 2 - i ) ) { for ( let k = i - 1 ; k >= 0 ; k -- ) { if ( s [ k ] != s [ j ] ) break ; count ++ ; } count ++ ; } if ( isPalindrome ( s , i + 1 , n - 1 - i ) ) { for ( let k = n - i ; k < n ; k ++ ) { if ( s [ k ] != s [ i ] ) break ; count ++ ; } count ++ ; } break ; } } } return count ; } let s = " " ; document . write ( countWays ( s ) ) ;