iterate and mark the frequencies of all characters in str1
for ( int i = 0 ; i < l1 ; i ++ ) freq [ str1 [ i ] - ' a ' ] += 1 ; for ( int i = 0 ; i < l2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] += 1 ; int count = INT_MAX ;