Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | Function that returns true if s is palindrome ; Function to return the required index ; Copy the ith character in S ; Copy all the character of string s2 in Temp ; Check whether the string is palindrome ; Driver code
function isPalindrome ( s ) { let i = 0 ; let j = s . length - 1 ; while ( i < j ) { if ( s [ i ] != s [ j ] ) return false ; i ++ ; j -- ; } return true ; } function getIndex ( S1 , S2 , n ) { let S = " " ; for ( let i = 0 ; i < n ; i ++ ) { S = S + S1 [ i ] ; let Temp = " " ; for ( let j = i + 1 ; j < n ; j ++ ) Temp += S2 [ j ] ; if ( isPalindrome ( S + Temp ) ) { return i ; } } return - 1 ; } let S1 = " " , S2 = " " ; let n = S1 . length ; document . write ( getIndex ( S1 , S2 , n ) ) ;