Check if two strings can be made equal by swapping one character among each other | Function that returns true if the string can be made equal after one swap ; A and B are new a and b after we omit the same elements ; Take only the characters which are different in both the strings for every pair of indices ; If the current characters differ ; The strings were already equal ; If the lengths of the strings are two ; If swapping these characters can make the strings equal ; Driver code
def canBeEqual ( a , b , n ) : NEW_LINE INDENT A = [ ] NEW_LINE B = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] != b [ i ] : NEW_LINE INDENT A . append ( a [ i ] ) NEW_LINE B . append ( b [ i ] ) NEW_LINE DEDENT DEDENT if len ( A ) == len ( B ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT if len ( A ) == len ( B ) == 2 : NEW_LINE INDENT if A [ 0 ] == A [ 1 ] and B [ 0 ] == B [ 1 ] : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT A = ' SEEKSFORGEEKS ' NEW_LINE B = ' GEEKSFORGEEKG ' NEW_LINE if ( canBeEqual ( A , B , len ( A ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT