去重,然后暴力即可,去重时别忘了记录原来的长度
class Solution { public: int max(int a, int b) { return a > b ? a : b; } int maxProduct(vector<string>& words) { int len = words.size(); vector<string> ww; int a[1001]; bool vis[26]; bool vv[1001][26]; memset(vv, 0, sizeof(vv)); for(int i = 0; i < len; i++) { string temp = ""; memset(vis, 0, sizeof(vis)); a[i] = words[i].length(); for(int j = 0; j < a[i]; j++) if(vis[words[i][j] - 'a'] == 0) { temp += words[i][j], vis[words[i][j] - 'a'] = 1; vv[i][words[i][j] - 'a'] = 1; } ww.push_back(temp); } int sum = 0; for(int i = 0; i < len; i++) { string temp = ww[i]; int len1 = temp.length(); bool flag = 0; for(int j = i + 1; j < len; j++) { flag = 0; for(int k = 0; k < len1; k++) { if(vv[j][temp[k] - 'a']) { flag = 1; break; } } if(flag == 0) sum = max(sum, a[j] * a[i]); } } return sum; } };