Total length of string from given Array of strings composed using given characters | Java implementation to find total length of string composed of given characters formed from given Array of strings ; Function to count the total length ; Map 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
import java . util . * ; class GFG { static int countCharacters ( List < String > strings , String chars ) { int res = 0 ; Map < Character , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < chars . length ( ) ; i ++ ) { freq . put ( chars . charAt ( i ) , freq . getOrDefault ( chars . charAt ( i ) , 0 ) + 1 ) ; } for ( String st : strings ) { boolean flag = true ; for ( char c : st . toCharArray ( ) ) { if ( ! freq . containsKey ( c ) ) { flag = false ; break ; } } if ( flag ) res += st . length ( ) ; } return res ; } public static void main ( String [ ] args ) { List < String > strings = Arrays . asList ( " hi " , " data " , " geeksforgeeks " ) ; String chars = " tiadhae " ; System . out . println ( countCharacters ( strings , chars ) ) ; } }