This portion deals with the lexicographical property . Now , we put a character in string X when either this character has smaller value than the character present there right now or if this is the last position for it to exchange , else we fix the character already present here in this position .
if countx [ ord ( x [ i ] ) - ord ( ' A ' ) ] == ctrx [ ord ( x [ i ] ) - ord ( ' A ' ) ] or ord ( x [ i ] ) - ord ( ' A ' ) > j : NEW_LINE INDENT countx [ ord ( x [ i ] ) - ord ( ' A ' ) ] -= 1 NEW_LINE ctrx [ ord ( x [ i ] ) - ord ( ' A ' ) ] -= 1 NEW_LINE ctry [ j ] -= 1 NEW_LINE x [ i ] = chr ( ord ( ' A ' ) + j ) NEW_LINE DEDENT else : NEW_LINE INDENT countx [ ord ( x [ i ] ) - ord ( ' A ' ) ] -= 1 NEW_LINE DEDENT print ( " Anagram ▁ : " , ' ' . join ( x ) ) NEW_LINE print ( " Number ▁ of ▁ changes ▁ made ▁ : " , change // 2 ) NEW_LINE