human
What is a code to find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated and the other steps required given the following:
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | Function that returns true if the sub - string starting from index i and ending at index j is a palindrome.
Function to get the required index.
Start comparing the two strings from both ends ..
Break from the loop at first mismatch.
If it is possible to concatenate the strings to form palindrome , return index.
If remaining part for s2 is palindrome.
If remaining part for s1 is palindrome.
If not possible , return - 1.
Driver Code"
Solution in Python:
assistant
#Here is code to find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated and the other steps required.
 def isPalindrome ( s , i , j ) :
	while ( i < j ) :
		if ( s [ i ] != s [ j ] ) :
			return False ;
		i += 1 ;
		j -= 1 ;
	return True ;
def getIndex ( s1 , s2 , length ) :
	i = 0 ; j = length - 1 ;
	while ( i < j ) :
		if ( s1 [ i ] != s2 [ j ] ) :
			break ;
		i += 1 ;
		j -= 1 ;
	if ( i == j ) :
		return i - 1 ;
	elif ( isPalindrome ( s2 , i , j ) ) :
		return i - 1 ;
	elif ( isPalindrome ( s1 , i , j ) ) :
		return j ;
	return - 1 ;
if __name__ == " _ _ main _ _ " :
	s1 = " abcdf " ;
	s2 = " sfgba " ;
	length = len ( s1 ) ;
	print ( getIndex ( s1 , s2 , length ) ) ;