<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>

from collections import defaultdict

class Solution:
    def wordSubsets(self, words_1, words_2):
        hashed_dict_to_dict_map_2 = defaultdict(dict)
        words_2_set = set(words_2)
        sorted_words_2_set = set()
        for word_2 in words_2_set:
            hashed_dict_2 = self.get_hashed_dict(word_2)
            if (hashed_dict_2 not in hashed_dict_to_dict_map_2.keys()):
                hashed_dict_to_dict_map_2[hashed_dict_2] = self.word_to_dict(word_2)
        
        ret_val = []
        hashed_dict_to_is_universal_map_1 = {}
        for word_1 in words_1:
            hashed_dict_1 = self.get_hashed_dict(word_1)
            if (hashed_dict_1 not in hashed_dict_to_is_universal_map_1.keys()):
                char_to_freq_map_1 = self.word_to_dict(word_1)
                hashed_dict_to_is_universal_map_1[hashed_dict_1] = self.is_universal(char_to_freq_map_1, hashed_dict_to_dict_map_2)

            if (hashed_dict_1 in hashed_dict_to_is_universal_map_1.keys() and hashed_dict_to_is_universal_map_1[hashed_dict_1]):
                ret_val.append(word_1)
    
        return ret_val

    def is_universal(self, char_to_freq_map_1, hashed_dict_to_dict_map_2):
        is_universal = True

        for hashed_dict_2 in hashed_dict_to_dict_map_2.keys():
            char_to_freq_map_2 = hashed_dict_to_dict_map_2[hashed_dict_2]
            if (not self.is_subset(char_to_freq_map_1, char_to_freq_map_2)):
                is_universal = False

        return is_universal

    def word_to_dict(self, word):
        ret_val = defaultdict(int)
        for c in sorted(word):
            ret_val[c] += 1
        return ret_val

    def get_hashed_dict(self, word):
        ret_val = '{'
        char_to_freq_map = self.word_to_dict(word)
        for c in sorted(char_to_freq_map):
            freq = char_to_freq_map[c]
            ret_val += c+':'+str(freq)
        ret_val += '}'
        return ret_val

    def is_subset(self, char_to_freq_map_1, char_to_freq_map_2):
        for c_2 in char_to_freq_map_2.keys():
            if (c_2 not in char_to_freq_map_1.keys()):
                return False
            else:
                if (char_to_freq_map_2[c_2] > char_to_freq_map_1[c_2]):
                    return False
                
        return True
20600