Count common characters in two strings | Function to return the count of valid indices pairs ; To store the frequencies of characters of string s1 and s2 ; To store the count of valid pairs ; Update the frequencies of the characters of string s1 ; Update the frequencies of the characters of string s2 ; Find the count of valid pairs ; Driver code
function countPairs ( s1 , n1 , s2 , n2 ) { let freq1 = new Array ( 26 ) ; let freq2 = new Array ( 26 ) ; freq1 . fill ( 0 ) ; freq2 . fill ( 0 ) ; let i , count = 0 ; for ( i = 0 ; i < n1 ; i ++ ) freq1 [ s1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( i = 0 ; i < n2 ; i ++ ) freq2 [ s2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( i = 0 ; i < 26 ; i ++ ) count += ( Math . min ( freq1 [ i ] , freq2 [ i ] ) ) ; return count ; } let s1 = " " , s2 = " " ; let n1 = s1 . length , n2 = s2 . length ; document . write ( countPairs ( s1 , n1 , s2 , n2 ) ) ;