# Count of strings that contains every String of another Array as Subset

• Last Updated : 12 Sep, 2022

Given two arrays of strings A[] and B[] containing only small case letters, the task is to count the number of strings in A[] such that it contains every string of array B[] as a subset (ordering of characters does not matter).

Examples:

Input: A[] = {“geeks”, “gfg”}, B[] = {“g”, “gf”}
Output: 1
Explanation: “gfg” in array A[] is the only string
that has both “g” and “gf”.

Input: A[] = {“geekolymic”, “google”, “amazon”},  B[] = {“og” }
Output:
Explanation: “geekolymic” and “google” both contains “og” as subset.

Approach:

The condition for all strings of B to be present as a subset in a string of array A is:

• Maximum frequency of every letter of B ≤ frequency of every letter of B in A[i].

Follow the steps to solve this problem:

• Create a max_freq[] array to store the maximum frequency of each letter in words of B.
• For each word in B, calculate the frequency of each letter in that word and update the max_freq[] array.
• Initialize a variable ans = 0, now for every word in A[], calculate the frequency of each character in a temp[] array.
• For each letter in that word if max_freq[i] <= temp[i], increment the ans.
• Return the ans.

Below is the implementation of the above approach :

## C++14

 `// C++ code to implement the approach` `#include ` `using` `namespace` `std;`   `// Function to calculate the number of words` `// in A, that has every word of B as subset` `int` `countcommon(vector A, vector B, ``int` `N,` `                ``int` `M)` `{`   `    ``// array to store maximum frequency of` `    ``// characters of array B count of B[i]` `    ``// which matches the condition` `    ``int` `max_freq = { 0 };` `    ``int` `ans = 0;` `    ``for` `(``int` `i = 0; i < M; i++) {` `        ``int` `temp = { 0 };` `        ``for` `(``int` `j = 0; j < B[i].size(); j++) {` `            ``temp[B[i][j] - ``'a'``]++;` `        ``}` `        ``for` `(``int` `k = 0; k < 26; k++) {` `            ``max_freq[k] = max(max_freq[k], temp[k]);` `        ``}` `    ``}`   `    ``// Iterate in array a and check for condition` `    ``// max_freq[i]<=frequency of every a[i][j]` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``int` `temp = { 0 };` `        ``for` `(``int` `j = 0; j < A[i].size(); j++) {` `            ``temp[A[i][j] - ``'a'``]++;` `        ``}` `        ``bool` `f = ``false``;` `        ``for` `(``int` `k = 0; k < 26; k++) {` `            ``if` `(max_freq[k] > temp[k]) {` `                ``f = ``true``;` `                ``break``;` `            ``}` `        ``}` `        ``if` `(f == ``false``)` `            ``ans++;` `    ``}`   `    ``// Return the answer` `    ``return` `ans;` `}`   `// Driver Code` `int` `main()` `{` `    ``vector A{ ``"geekolymic"``, ``"google"``, ``"amazon"` `};` `    ``vector B{ ``"go"` `};` `    ``int` `N = A.size();` `    ``int` `M = B.size();` `    ``cout << countcommon(A, B, N, M) << endl;` `    ``return` `0;` `}`

## Java

 `/*package whatever //do not write package name here */`   `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG {` `  `  `// Function to calculate the number of words` `// in A, that has every word of B as subset` `static` `int` `countcommon(String[] A, String[] B, ``int` `N, ``int` `M)` `{`   `    ``// array to store maximum frequency of` `    ``// characters of array B count of B[i]` `    ``// which matches the condition` `    ``int` `max_freq[] = ``new` `int``[``26``];` `    ``int` `ans = ``0``;` `    ``for` `(``int` `i = ``0``; i < M; i++) {` `        ``int` `temp[] = ``new` `int``[``26``];` `        ``for` `(``int` `j = ``0``; j < B[i].length(); j++) {` `            ``temp[B[i].charAt(j) - ``'a'``]++;` `        ``}` `        ``for` `(``int` `k = ``0``; k < ``26``; k++) {` `            ``max_freq[k] = Math.max(max_freq[k], temp[k]);` `        ``}` `    ``}`   `    ``// Iterate in array a and check for condition` `    ``// max_freq[i]<=frequency of every a[i][j]` `    ``for` `(``int` `i = ``0``; i < N; i++) {` `        ``int` `temp[] = ``new` `int``[``26``];` `        ``for` `(``int` `j = ``0``; j < A[i].length(); j++) {` `            ``temp[A[i].charAt(j) - ``'a'``]++;` `        ``}` `        ``boolean` `check = ``false``;` `        ``for` `(``int` `k = ``0``; k < ``26``; k++) {` `            ``if` `(max_freq[k] > temp[k]) {` `                ``check = ``true``;` `                ``break``;` `            ``}` `        ``}` `        ``if` `(!check)` `            ``ans++;` `    ``}`   `    ``// Return the answer` `    ``return` `ans;` `}` `  `  `    ``public` `static` `void` `main (String[] args) {` `        ``String A[] = { ``"geekolymic"``, ``"google"``, ``"amazon"` `};` `        ``String B[] = { ``"go"` `};` `        ``int` `N = A.length;` `        ``int` `M = B.length;` `        ``System.out.println(countcommon(A, B, N, M));` `    ``}` `}`   `// This code is contributed by aadityapburujwale`

## Python3

 `# python3 code to implement the approach`   `# Function to calculate the number of words` `# in A, that has every word of B as subset` `from` `random ``import` `randrange`     `def` `countcommon(A, B, N, M):`   `    ``# array to store maximum frequency of` `    ``# characters of array B count of B[i]` `    ``# which matches the condition` `    ``max_freq ``=` `[``0` `for` `_ ``in` `range``(``26``)]` `    ``ans ``=` `0` `    ``for` `i ``in` `range``(``0``, M):` `        ``temp ``=` `[``0` `for` `_ ``in` `range``(``26``)]` `        ``for` `j ``in` `range``(``0``, ``len``(B[i])):` `            ``temp[``ord``(B[i][j]) ``-` `ord``(``'a'``)] ``+``=` `1`   `        ``for` `k ``in` `range``(``0``, ``26``):` `            ``max_freq[k] ``=` `max``(max_freq[k], temp[k])`   `    ``# Iterate in array a and check for condition` `    ``# max_freq[i]<=frequency of every a[i][j]` `    ``for` `i ``in` `range``(``0``, N):` `        ``temp ``=` `[``0` `for` `_ ``in` `range``(``26``)]` `        ``for` `j ``in` `range``(``0``, ``len``(A[i])):` `            ``temp[``ord``(A[i][j]) ``-` `ord``(``'a'``)] ``+``=` `1`   `        ``f ``=` `False` `        ``for` `k ``in` `range``(``0``, k):` `            ``if` `(max_freq[k] > temp[k]):` `                ``f ``=` `True` `                ``break`   `        ``if` `(f ``=``=` `False``):` `            ``ans ``+``=` `1`   `    ``# Return the answer` `    ``return` `ans`     `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``A ``=` `[``"geekolymic"``, ``"google"``, ``"amazon"``]` `    ``B ``=` `[``"go"``]` `    ``N ``=` `len``(A)` `    ``M ``=` `len``(B)` `    ``print``(countcommon(A, B, N, M))`   `    ``# This code is contributed by rakeshsahni`

## C#

 `/*package whatever //do not write package name here */`     `using` `System;`   `public` `class` `GFG {` `  `  `// Function to calculate the number of words` `// in A, that has every word of B as subset` `static` `int` `countcommon(String[] A, String[] B, ``int` `N, ``int` `M)` `{`   `    ``// array to store maximum frequency of` `    ``// characters of array B count of B[i]` `    ``// which matches the condition` `    ``int` `[]max_freq = ``new` `int``;` `    ``int` `ans = 0;` `    ``for` `(``int` `i = 0; i < M; i++) {` `        ``int` `[]temp = ``new` `int``;` `        ``for` `(``int` `j = 0; j < B[i].Length; j++) {` `            ``temp[B[i][j] - ``'a'``]++;` `        ``}` `        ``for` `(``int` `k = 0; k < 26; k++) {` `            ``max_freq[k] = Math.Max(max_freq[k], temp[k]);` `        ``}` `    ``}`   `    ``// Iterate in array a and check for condition` `    ``// max_freq[i]<=frequency of every a[i,j]` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``int` `[]temp = ``new` `int``;` `        ``for` `(``int` `j = 0; j < A[i].Length; j++) {` `            ``temp[A[i][j] - ``'a'``]++;` `        ``}` `        ``bool` `check = ``false``;` `        ``for` `(``int` `k = 0; k < 26; k++) {` `            ``if` `(max_freq[k] > temp[k]) {` `                ``check = ``true``;` `                ``break``;` `            ``}` `        ``}` `        ``if` `(!check)` `            ``ans++;` `    ``}`   `    ``// Return the answer` `    ``return` `ans;` `}` `  `  `    ``public` `static` `void` `Main(String[] args) {` `        ``String []A = { ``"geekolymic"``, ``"google"``, ``"amazon"` `};` `        ``String []B = { ``"go"` `};` `        ``int` `N = A.Length;` `        ``int` `M = B.Length;` `        ``Console.WriteLine(countcommon(A, B, N, M));` `    ``}` `}`     `// This code contributed by shikhasingrajput`

## Javascript

 ``

Output

`2`

Time Complexity: O(max(N, M)*26), where N is the size of A and M is the size of B
Auxiliary Space: O(26)

My Personal Notes arrow_drop_up
Recommended Articles
Page :