Count strings from given array having all characters appearing in a given string | C ++ program to implement the above approach ; Function to count the number of strings from an array having all characters appearing in the string S ; Initialize a set to store all distinct characters of string S ; Traverse over string S ; Insert characters into the Set ; Stores the required count ; Traverse the array ; Traverse over string arr [ i ] ; Check if character in arr [ i ] [ j ] is present in the string S or not ; Increment the count if all the characters of arr [ i ] are present in the string S ; Finally , print the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countStrings ( string S , vector < string > & list ) { unordered_set < char > valid ; for ( auto x : S ) { valid . insert ( x ) ; } int cnt = 0 ; for ( int i = 0 ; i < list . size ( ) ; i ++ ) { int j = 0 ; for ( j = 0 ; j < list [ i ] . size ( ) ; j ++ ) { if ( valid . count ( list [ i ] [ j ] ) ) continue ; else break ; } if ( j == list [ i ] . size ( ) ) cnt ++ ; } return cnt ; } int main ( ) { vector < string > arr = { " ab " , " aab " , " abaaaa " , " bbd " } ; string S = " ab " ; cout << countStrings ( S , arr ) << endl ; }