Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | C ++ Program to find minimum number of manipulations required to make two strings identical ; Counts the no of manipulations required ; store the count of character ; iterate though the first String and update count ; iterate through the second string update char_count . if character is not found in char_count then increase count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countManipulations ( string s1 , string s2 ) { int count = 0 ; int char_count [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { char_count [ i ] = 0 ; } for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) char_count [ s1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { char_count [ s2 [ i ] - ' a ' ] -- ; } for ( int i = 0 ; i < 26 ; ++ i ) { if ( char_count [ i ] != 0 ) { count += abs ( char_count [ i ] ) ; } } return count / 2 ; } int main ( ) { string s1 = " ddcf " ; string s2 = " cedk " ; cout << countManipulations ( s1 , s2 ) ; }