<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:
    int maxProduct(vector<string>& words) {
         vector<int> lenOfWords;

        for(int index = 0;index<words.size();index++) {
            sort(words[index].begin(), words[index].end());
            lenOfWords.push_back(words[index].size());
        } 

        vector<vector<int>> disjointPairs(words.size(), vector<int>(words.size()));

        for(int i = 0;i<26;i++) {
            vector<int> disjointPairsForIthChar;
            for(int index = 0;index<words.size();index++) {
                if(words[index].size()>0 and words[index][0] == 'a'+i) {
                    disjointPairsForIthChar.push_back(index);
                }

                while(words[index].size()>0 and words[index][0] == 'a'+i) {
                    words[index].erase(0, 1);
                }
            }
       
            for(int first = 0;first<disjointPairsForIthChar.size();first++) {
                for(int second = 0;second<disjointPairsForIthChar.size(); second++) {
                    disjointPairs[disjointPairsForIthChar[first]][disjointPairsForIthChar[second]] = 1;
                }
            }
            disjointPairsForIthChar.clear();
        }

        int maxProduct = 0;
        for(int first = 0;first<words.size();first++) {
            for(int second = first+1; second<words.size(); second++) {
                if(disjointPairs[first][second]==0 and disjointPairs[second][first]==0) {
                    maxProduct = max(maxProduct, lenOfWords[first]*lenOfWords[second]);
                }
            }
        }

        return maxProduct;
    }
};
68655