Minimize replacements or swapping of same indexed characters required to make two given strings palindromic | Function to find minimum operations to make both the strings palindromic ; Stores index of the left pointer ; Stores index of the right pointer ; Stores count of minimum operations to make both the strings palindromic ; if str1 [ i ] equal to str1 [ j ] and str2 [ i ] not equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] not equal to str1 [ j ] and str2 [ i ] equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] is not equal to str1 [ j ] and str2 [ i ] is not equal to str2 [ j ] ; If str1 [ i ] is equal to str2 [ j ] and str2 [ i ] is equal to str1 [ j ] ; Update cntOp ; Update cntOp ; Update i and j ; Driver Code ; Stores length of str1
def MincntBothPalin ( str1 , str2 , N ) : NEW_LINE INDENT i = 0 NEW_LINE j = N - 1 NEW_LINE cntOp = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( str1 [ i ] == str1 [ j ] and str2 [ i ] != str2 [ j ] ) : NEW_LINE INDENT cntOp += 1 NEW_LINE DEDENT elif ( str1 [ i ] != str1 [ j ] and str2 [ i ] == str2 [ j ] ) : NEW_LINE INDENT cntOp += 1 NEW_LINE DEDENT elif ( str1 [ i ] != str1 [ j ] and str2 [ i ] != str2 [ j ] ) : NEW_LINE INDENT if ( str1 [ i ] == str2 [ j ] and str2 [ i ] == str1 [ j ] ) : NEW_LINE INDENT cntOp += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntOp += 2 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return cntOp NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " dbba " NEW_LINE str2 = " abcd " NEW_LINE N = len ( str1 ) NEW_LINE print ( MincntBothPalin ( str1 , str2 , N ) ) NEW_LINE DEDENT