<p>Given a string array <code>words</code>, return <em>the maximum value of</em> <code>length(word[i]) * length(word[j])</code> <em>where the two words do not share common letters</em>. If no such two words exist, return <code>0</code>.</p>

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

<pre>
<strong>Input:</strong> words = [&quot;abcw&quot;,&quot;baz&quot;,&quot;foo&quot;,&quot;bar&quot;,&quot;xtfn&quot;,&quot;abcdef&quot;]
<strong>Output:</strong> 16
<strong>Explanation:</strong> The two words can be &quot;abcw&quot;, &quot;xtfn&quot;.
</pre>

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

<pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;ab&quot;,&quot;abc&quot;,&quot;d&quot;,&quot;cd&quot;,&quot;bcd&quot;,&quot;abcd&quot;]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The two words can be &quot;ab&quot;, &quot;cd&quot;.
</pre>

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

<pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;aa&quot;,&quot;aaa&quot;,&quot;aaaa&quot;]
<strong>Output:</strong> 0
<strong>Explanation:</strong> No such pair of words.
</pre>

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

<ul>
	<li><code>2 &lt;= words.length &lt;= 1000</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 1000</code></li>
	<li><code>words[i]</code> consists only of lowercase English letters.</li>
</ul>

class Solution {
public:

    struct Trie{
        map<int, Trie> child;
        bool flag;
        int size;
        Trie() {
            flag = 0;
            size = 0;
        }
    
        void addWord(int mask, int pos, int sz) {
            if(pos == 26) {
                flag = 1;
                size = max(size, sz);
                return;
            }
            int curr = ((mask & (1 << pos)) != 0);
            if(child.find(curr) == child.end()){
                child[curr] = Trie();
            }
            child[curr].addWord(mask, pos + 1, sz);
        }
        
        int find(int mask, int pos){
            if(pos == 26){
                return size;
            }else{
                int ans = 0;
                if((mask & (1 << pos)) == 0){
                    if(child.find(1) != child.end()){
                        ans = max(ans, child[1].find(mask, pos+1));
                    }
                }
                if(child.find(0) != child.end()){
                    ans = max(ans, child[0].find(mask, pos+1));
                }
                return ans;
            }
        }
    };
    
    vector<int> masks;
    
    int maxProduct(vector<string>& words) {
        Trie tree;
        for(int i = 0; i < words.size(); i++){
            int mask = 0;
            for(int j = 0; j < words[i].size(); j++){
                mask |= (1 << (words[i][j] - 'a'));
            }
            masks.push_back(mask);
            tree.addWord(mask,0,words[i].size());
        }
        long long ans = 0;
        for(int i = 0; i < words.size(); i++){
            int val = tree.find(masks[i], 0);
            ans = max((long long) words[i].size() * val, ans);
        }
        return ans;
    }
};
64876