<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 {
    vector<vector<int>>dp;
    unordered_map<string,int>masking;
public:
    Solution(){
        ios_base :: sync_with_stdio(false);
    }
    int solve(vector<string>& words,int start,int end){
        if(start >= end){
            return 0;
        }
        if(dp[start][end] != -1){
            return dp[start][end];
        }
        bool found = masking[words[start]] & masking[words[end]];
        if(!found){
            int size1 = words[start].size();
            int size2 = words[end].size();
            return dp[start][end] = max(size1*size2,max(solve(words,start+1,end-1),max(solve(words,start+1,end),solve(words,start,end-1))));
        }
        else{
        return dp[start][end] = max(0,max(solve(words,start+1,end-1),max(solve(words,start+1,end),solve(words,start,end-1))));
        }
    }
    int maxProduct(vector<string>& words) {
        int n = words.size();
        dp.resize(n+1,vector<int>(n+1,-1));
        for(int i = 0;i<n;i++){
            int bitMasking = 0;
            for(int j = 0;j<words[i].size();j++){
                bitMasking = (bitMasking | (1<<(words[i][j]-'a')));
            }
            masking[words[i]] = bitMasking;
        }
        return solve(words,0,words.size()-1);
    }
};
68655