<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 {
private:
    vector<int> wordsIndex[26];

    string getUniqueChars(string& str) {
        bool hsh[26] = {0};
        for (int i = 0 ; i < str.length() ; i++) {
            hsh[str[i]-'a'] = true;
        }

        string uniqChars;
        for (int i = 0 ; i < 26 ; i++) {
            if (hsh[i]) {
                uniqChars += ('a'+i);
            }
        }

        return uniqChars;
    } 
    int computeMaxProduct(string& strchars, vector<string>& words, int len) {
        bool hsh[words.size()];
        memset(hsh, 0, sizeof(hsh));
        for (int i = 0 ; i < strchars.length() ; i++) {
            for (int j = 0 ; j < wordsIndex[strchars[i]-'a'].size() ; j++) {
                hsh[wordsIndex[strchars[i]-'a'][j]] = true;
            }
        }

        int maxProd = 0;
        for (int i = 0 ; i < words.size() ; i++) {
            if (!hsh[i]) {
                int prod = words[i].length()*len;
                maxProd = max(maxProd, prod);
            }
        }

        return maxProd;
    }    
public:
    int maxProduct(vector<string>& words) {
        string wordcharStr[words.size()];
        for (int i = 0 ; i < words.size() ; i++) {
            wordcharStr[i] = getUniqueChars(words[i]);

            for (int j = 0 ; j < wordcharStr[i].length() ; j++) {
                wordsIndex[wordcharStr[i][j]-'a'].push_back(i);    
            }
        }

        int ans = 0;
        for (int i = 0 ; i < words.size() ; i++) {
            ans = max(ans, computeMaxProduct(wordcharStr[i], words, words[i].length()));
        }

        return ans;
    }
};
30868