text: Length of longest common prefix possible by rearranging strings in a given array | C # program to implement the above approach ; Function to get the length of the longest common prefix by rearranging the Strings ; freq [ i , j ] : stores the frequency of a character ( = j ) in a String arr [ i ] ; Traverse the given array ; Stores length of current String ; Traverse current String of the given array ; Update the value of freq [ i , arr [ i , j ] ] ; Stores the length of longest common prefix ; Count the minimum frequency of each character in in all the Strings of [ ] arr ; Stores minimum value in each row of [ , ] freq ; Calculate minimum frequency of current character in all the Strings . ; Update minRowVal ; Update maxLen ; Driver Code
code: using System ; class GFG { static int longComPre ( String [ ] arr , int N ) { int [ , ] freq = new int [ N , 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { int M = arr [ i ] . Length ; for ( int j = 0 ; j < M ; j ++ ) { freq [ i , arr [ i ] [ j ] ] ++ ; } } int maxLen = 0 ; for ( int j = 0 ; j < 256 ; j ++ ) { int minRowVal = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { minRowVal = Math . Min ( minRowVal , freq [ i , j ] ) ; } maxLen += minRowVal ; } return maxLen ; } public static void Main ( String [ ] args ) { String [ ] arr = { " aabdc " , " abcd " , " aacd " } ; int N = 3 ; Console . Write ( longComPre ( arr , N ) ) ; } }