Count common characters in two strings | C # implementation of the approach ; 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
using System ; class GFG { static int countPairs ( string s1 , int n1 , string s2 , int n2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; Array . Fill ( freq1 , 0 ) ; Array . Fill ( freq2 , 0 ) ; int i , count = 0 ; for ( i = 0 ; i < n1 ; i ++ ) freq1 [ s1 [ i ] - ' a ' ] ++ ; for ( i = 0 ; i < n2 ; i ++ ) freq2 [ s2 [ i ] - ' a ' ] ++ ; for ( i = 0 ; i < 26 ; i ++ ) count += ( Math . Min ( freq1 [ i ] , freq2 [ i ] ) ) ; return count ; } public static void Main ( ) { string s1 = " geeksforgeeks " , s2 = " platformforgeeks " ; int n1 = s1 . Length , n2 = s2 . Length ; Console . WriteLine ( countPairs ( s1 , n1 , s2 , n2 ) ) ; } }