You are given two string arrays words1 and words2.

A string b is a subset of string a if every letter in b occurs in a including multiplicity.


	For example, "wrr" is a subset of "warrior" but is not a subset of "world".


A string a from words1 is universal if for every string b in words2, b is a subset of a.

Return an array of all the universal strings in words1. You may return the answer in any order.

&nbsp;
Example 1:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]


Example 2:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
Output: ["apple","google","leetcode"]


&nbsp;
Constraints:


	1 &lt;= words1.length, words2.length &lt;= 104
	1 &lt;= words1[i].length, words2[i].length &lt;= 10
	words1[i] and words2[i] consist only of lowercase English letters.
	All the strings of words1 are unique.


class Solution:
    def wordSubsets(self, A: List[str], B: List[str]) -> List[str]:
        freq = [0]*26 
        
        for w in B: 
            temp = [0]*26
            for c in w: temp[ord(c)-97] += 1
            for i in range(26): freq[i] = max(freq[i], temp[i])
                
        ans = []
        for w in A: 
            temp = [0]*26
            for c in w: temp[ord(c)-97] += 1
            if all(freq[i] <= temp[i] for i in range(26)): ans.append(w)
        return ans 
class Solution {
    public List<String> wordSubsets(String[] words1, String[] words2) {
        List<String> list=new ArrayList<>();
        int[] bmax=count("");
        for(String w2:words2)
        {
            int[] b=count(w2);
            for(int i=0;i<26;i++)
            {
                bmax[i]=Math.max(bmax[i],b[i]);
            }
        }
        for(String w1:words1)
        {
            int[] a=count(w1);
            for(int i=0;i<26;i++)
            {
                if(a[i]<bmax[i])
                {
                    break;
                }
                  if(i==25)
             {
                 list.add(w1);
             }
            } 
        }
        return list;
    }
    public int[] count(String s)
    {
        int[] ans=new int[26];
        for(char c:s.toCharArray())
        {
            ans[c-'a']++;
        }
        return ans;
    }
}
class Solution {
public:
    // calculate the frequency of string s
    vector<int> giveMeFreq(string s)
    {
        vector<int> freq(26,0);
        for(int i = 0; i < s.length(); i++)
        {
            freq[s[i] - 'a']++;
        }
        return freq;
    }
    
    vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) 
    {
       vector<string> ans; // store ans
       vector<int> max_Freq_w2(26, 0);   // store max freq of each character present in word2 stirngs
	   
        // we will Iterate over word to and try to find max freq for each character present in all strings.
		for(auto &x : words2) 
        {
            vector<int> freq = giveMeFreq(x);
            for(int i = 0; i < 26; i++)
            {
                max_Freq_w2[i] = max(freq[i], max_Freq_w2[i]);  // upadate freq to max freq
            }
        }
        
		// we will iterate for each string in words1 ans if it have all charaters present in freq array with freq >= that     then we will add it to ans
        for(auto &x : words1)
        {
            vector<int> freq = giveMeFreq(x);  // gives freq of characters for word in words1
            bool flag = true;
            for(int i = 0; i < 26; i++)
            {
                if(freq[i] < max_Freq_w2[i]) // specifies that word did not have all the characters from word2 array
                {
                    flag = false;
                    break;
                }
            }
            if(flag) ans.push_back(x);   // string x is Universal string
        }
        return ans;
    }
};
var wordSubsets = function(words1, words2) {
    this.count = Array(26).fill(0);
    let tmp = Array(26).fill(0);
    for(let b of words2){
        tmp = counter(b);
        for(let i=0; i<26; i++)
            count[i] = Math.max(count[i], tmp[i]);
    }
    let list = []
    for(let a of words1)
        if(isSub(counter(a)))
            list.push(a);
    return list;
};

function isSub(tmp){
    for(let i=0; i<26; i++)
        if(tmp[i] < this.count[i])
            return false;
    return true;
};

function counter(s){
    let tmp = Array(26).fill(0);
    for(let c of s)
        tmp[c.charCodeAt() - 97]++;
    return tmp;
};
Word Subsets