<p>You are given two string arrays <code>words1</code> and <code>words2</code>.</p>

<p>A string <code>b</code> is a <strong>subset</strong> of string <code>a</code> if every letter in <code>b</code> occurs in <code>a</code> including multiplicity.</p>

<ul>
	<li>For example, <code>&quot;wrr&quot;</code> is a subset of <code>&quot;warrior&quot;</code> but is not a subset of <code>&quot;world&quot;</code>.</li>
</ul>

<p>A string <code>a</code> from <code>words1</code> is <strong>universal</strong> if for every string <code>b</code> in <code>words2</code>, <code>b</code> is a subset of <code>a</code>.</p>

<p>Return an array of all the <strong>universal</strong> strings in <code>words1</code>. You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words1 = [&quot;amazon&quot;,&quot;apple&quot;,&quot;facebook&quot;,&quot;google&quot;,&quot;leetcode&quot;], words2 = [&quot;e&quot;,&quot;o&quot;]
<strong>Output:</strong> [&quot;facebook&quot;,&quot;google&quot;,&quot;leetcode&quot;]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words1 = [&quot;amazon&quot;,&quot;apple&quot;,&quot;facebook&quot;,&quot;google&quot;,&quot;leetcode&quot;], words2 = [&quot;l&quot;,&quot;e&quot;]
<strong>Output:</strong> [&quot;apple&quot;,&quot;google&quot;,&quot;leetcode&quot;]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words1.length, words2.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words1[i].length, words2[i].length &lt;= 10</code></li>
	<li><code>words1[i]</code> and <code>words2[i]</code> consist only of lowercase English letters.</li>
	<li>All the strings of <code>words1</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    vector<string> wordSubsets(vector<string>& A, vector<string>& B) 
    {
        vector<string> result;
        // Hash Table that stores characters and how many times it should exist in the words from A
        map<char, int> charToCheckB;
        map<char, int> temp;
        // Hash Table that stores characters and how many times it exists in the word from A
        map<char, int> charToCheckA;
        
        // Loop by each element from B
        for(int i = 0; i < B.size(); ++i)
        {
            // Loop by each character and count number of characters to temp
            for(int j = 0; j < B[i].length(); ++j)
            {
                // Count current characters
                temp[B[i][j]]++;
                
                // Put to Hash Table bigger number between previous and current counted
                if (temp[B[i][j]] > charToCheckB[B[i][j]]) charToCheckB[B[i][j]] = temp[B[i][j]];
            }
            // Clear temp HashTable for next step
            temp.clear();
        }
        
        // Loop by each element from A
        for(int i = 0; i < A.size(); ++i)
        {
            // Loop by each character and count number of characters
            for(int j = 0; j < A[i].length(); ++j)
            {
                charToCheckA[A[i][j]]++;
            }
            
            // Loop by charToCheckB and check numbers. If numbers more than in the word - switch flag
            bool isExist = true;
            for(auto it = charToCheckB.begin(); it != charToCheckB.end(); ++it )
            {
                // If number of characters from HT B > num. of char. of word - char. doesn't exist in the word
                if ( it->second > charToCheckA[it->first] )
                {
                    isExist = false;
                    break;
                }
            }
            
            // Only when all character from B was found in A - add word to result
            if(isExist == true)
            {
                result.push_back(A[i]);
            }
            // Clear HashTable for words for next step
            charToCheckA.clear();
        }
        
        return result;
    }
};
168200