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