<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:
    static bool compareSecond(const pair<pair<int, int>, int>& a, const pair<pair<int, int>, int>& b) {
        return a.second > b.second;
    }

    int maxProduct(vector<string>& words) {
        vector<pair<int, int>> len(words.size(), {0, 0}); //fisrt: position, second: length

        for(int i = 0; i < words.size(); ++i) {
            len[i].first = i;
            len[i].second = words[i].size();
        }

        // for(auto i : len) {
        //     cout << i.first << " " << i.second << endl;
        // }

        vector<pair<pair<int, int>, int>> timesVal;

        for(int i = 0; i < len.size() - 1; ++i) {
            for(int j = i + 1; j < len.size(); ++j) {
                timesVal.push_back({{i, j}, len[i].second * len[j].second});
            }
        }

        sort(timesVal.begin(), timesVal.end(), compareSecond);

        // for(int i = 0; i < timesVal.size(); ++i) {
        //     cout << timesVal[i].first.first << " " << timesVal[i].first.second << " " << timesVal[i].second << endl;
        // }

        int answer = 0;
        bool valid = true;

        for(int i = 0; i < timesVal.size(); ++i) {
            // cout << words[timesVal[i].first.first] << endl;
            valid = true;
            for(int j = 0; j < words[timesVal[i].first.first].size(); ++j) {
                // cout <<
                if(words[timesVal[i].first.second].find(words[timesVal[i].first.first][j]) != string::npos) {
                    valid = false;
                    break;
                }
            }
            
            if(valid) {
                answer = timesVal[i].second;
                break;
            }
        }

        return answer;
    }
};
98885