Least number of manipulations needed to ensure two strings have identical characters | Python3 program to count least number of manipulations to have two strings set of same characters ; return the count of manipulations required ; count the number of different characters in both strings ; check the difference in characters by comparing count arrays ; Driver Code
MAX_CHAR = 26 NEW_LINE def leastCount ( s1 , s2 , n ) : NEW_LINE INDENT count1 = [ 0 ] * MAX_CHAR NEW_LINE count2 = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT count1 [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE count2 [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( MAX_CHAR ) : NEW_LINE INDENT if ( count1 [ i ] != 0 ) : NEW_LINE INDENT res += abs ( count1 [ i ] - count2 [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " abc " NEW_LINE s2 = " cdd " NEW_LINE l = len ( s1 ) NEW_LINE res = leastCount ( s1 , s2 , l ) NEW_LINE print ( res ) NEW_LINE DEDENT