Total length of string from given Array of strings composed using given characters | C # implementation to find total length of string composed of given characters formed from given Array of strings ; Function to count the total length ; Dictionary for keeping frequency of characters ; Calculate the frequency ; Iterate in the N strings ; Iterates in the string ; Checks if given character of string string appears in it or not ; Adds the length of string if all characters are present ; Return the final result ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int countCharacters ( List < string > strings , string chars ) { int res = 0 ; Dictionary < char , int > freq = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < chars . Length ; i ++ ) { if ( freq . ContainsKey ( chars [ i ] ) ) { freq [ chars [ i ] ] ++ ; } else { freq . Add ( chars [ i ] , freq . GetValueOrDefault ( chars [ i ] , 0 ) + 1 ) ; } } foreach ( string st in strings ) { bool flag = true ; foreach ( char c in st . ToCharArray ( ) ) { if ( ! freq . ContainsKey ( c ) ) { flag = false ; break ; } } if ( flag ) res += st . Length ; } return res ; } public static void Main ( string [ ] args ) { string [ ] tmp = { " hi " , " data " , " geeksforgeeks " } ; List < string > strings = tmp . ToList ( ) ; string chars = " tiadhae " ; Console . Write ( countCharacters ( strings , chars ) ) ; } }