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
< ? php function isPalindrome ( $ s ) { $ i = 0 ; $ j = strlen ( $ s ) - 1 ; while ( $ i < $ j ) { if ( $ s [ $ i ] != $ s [ $ j ] ) return false ; $ i ++ ; $ j -- ; } return true ; } function getIndex ( $ S1 , $ S2 , $ n ) { $ S = " " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ S = $ S . $ S1 [ $ i ] ; $ Temp = " " ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ Temp . = $ S2 [ $ j ] ; if ( isPalindrome ( $ S . $ Temp ) ) { return $ i ; } } return -1 ; } $ S1 = " abcdf " ; $ S2 = " sfgba " ; $ n = strlen ( $ S1 ) ; echo getIndex ( $ S1 , $ S2 , $ n ) ; ? >