Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Python3 program to implement the above approach ; iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; else we could just break the loop as its not a palindrome type sequence ; we could concatenate the a ' s ▁ left ▁ part ▁ ▁ + b ' s right part in a variable a and a ' s ▁ ▁ right ▁ part + b ' s left in the variable b ; we would check for the palindrome condition if yes we print True else False
def check ( a , b , n ) : NEW_LINE INDENT i , j = 0 , n - 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( a [ i ] != b [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE xa = a [ i : j + 1 ] NEW_LINE xb = b [ i : j + 1 ] NEW_LINE if ( xa == xa [ : : - 1 ] or xb == xb [ : : - 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT a = " xbdef " NEW_LINE b = " cabex " NEW_LINE if check ( a , b , len ( a ) ) == True or check ( b , a , len ( a ) ) == True : NEW_LINE INDENT print ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( False ) NEW_LINE DEDENT