Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Function to check if a string is palindrome or not ; Start and end of the string ; Iterate the string until i > j ; If there is a mismatch ; Increment first pointer and decrement the other ; Given string is a palindrome ; Function two check if the strings can be combined to form a palindrome ; Initialize array of characters ; Stores character of string in the character array ; Find left and right parts of strings a and b ; Substring a [ j ... i - 1 ] ; Substring b [ j ... i - 1 ] ; Substring a [ i ... n ] ; Substring b [ i ... n ] ; Check is left part of a + right part of b or vice versa is a palindrome ; Print the result ; Otherwise ; Driver Code
def isPalindrome ( st ) : NEW_LINE INDENT i = 0 NEW_LINE j = len ( st ) - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( st [ i ] != st [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def formPalindrome ( a , b , n ) : NEW_LINE INDENT aa = [ ' ▁ ' ] * ( n + 2 ) NEW_LINE bb = [ ' ▁ ' ] * ( n + 2 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT aa [ i ] = a [ i - 1 ] NEW_LINE bb [ i ] = b [ i - 1 ] NEW_LINE DEDENT ok = False NEW_LINE for i in range ( n + 2 ) : NEW_LINE INDENT la = " " NEW_LINE ra = " " NEW_LINE lb = " " NEW_LINE rb = " " NEW_LINE for j in range ( 1 , i ) : NEW_LINE INDENT if ( aa [ j ] == ' ▁ ' ) : NEW_LINE INDENT la += " " NEW_LINE DEDENT else : NEW_LINE INDENT la += aa [ j ] NEW_LINE DEDENT if ( bb [ j ] == ' ▁ ' ) : NEW_LINE INDENT lb += " " NEW_LINE DEDENT else : NEW_LINE INDENT lb += bb [ j ] NEW_LINE DEDENT DEDENT for j in range ( i , n + 2 ) : NEW_LINE INDENT if ( aa [ j ] == ' ▁ ' ) : NEW_LINE INDENT ra += " " NEW_LINE DEDENT else : NEW_LINE INDENT ra += aa [ j ] NEW_LINE DEDENT if ( bb [ j ] == ' ▁ ' ) : NEW_LINE INDENT rb += " " NEW_LINE DEDENT else : NEW_LINE INDENT rb += bb [ j ] NEW_LINE DEDENT DEDENT if ( isPalindrome ( str ( la ) + str ( rb ) ) or isPalindrome ( str ( lb ) + str ( ra ) ) ) : NEW_LINE INDENT ok = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( ok ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = " bdea " NEW_LINE B = " abbb " NEW_LINE N = 4 NEW_LINE formPalindrome ( A , B , N ) NEW_LINE DEDENT