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 ; Unordered_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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countCharacters ( vector < string > & strings , string chars ) { int res = 0 ; unordered_map < char , int > freq ; for ( int i = 0 ; i < chars . length ( ) ; i ++ ) freq [ chars [ i ] ] += 1 ; for ( auto st : strings ) { bool flag = true ; for ( auto c : st ) { if ( ! freq ) { flag = false ; break ; } } if ( flag ) res += st . length ( ) ; } return res ; } int main ( ) { vector < string > strings = { " hi " , " data " , " geeksforgeeks " } ; string chars = " tiadhae " ; cout << countCharacters ( strings , chars ) ; return 0 ; }