<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) {
        int length = words.size();
        //Get the maximum possible product of two lengths
        vector<Combi> sorted;
        for(int i = 0; i < length-1; i++){
            for(int j = i+1; j < length; j++){
                Combi newCombi(&words[i], &words[j]);
                sorted.push_back(newCombi);
            }
        }
        sort(sorted.begin( ), sorted.end( ), 
            [](const Combi& first, const Combi& second){
                return first.product > second.product;
            });
        //check each combination
        for(Combi c : sorted){
            if(bothStringsDestinctChars(*(c.word1), *(c.word2))) return c.product;
        }
        return 0;
    }

private:
    bool bothStringsDestinctChars(string& s1, string& s2){
        vector<bool> charWord1(26, false);
        for(char c : s1) charWord1[c-'a'] = true;
        for(char c : s2){
            if(charWord1[c-'a']) return false;
        }
        return true;
    }

    class Combi{
        public:
        int product;
        string* word1;
        string* word2;

        Combi(string* word1, string* word2){
            this->word1 = word1;
            this->word2 = word2;
            product = word1->size() * word2->size();
        } 
    };
};
121558