classSolution { public: boolhave_same_char(string str1, string str2){ for (char c = 'a'; c <= 'z'; ++c) { int flag1 = 0; int flag2 = 0; for (int i = 0; i < str1.size(); ++i) if (str1[i] == c) {flag1 = 1; break;} for (int i = 0; i < str2.size(); ++i) if (str2[i] == c) {flag2 = 1; break;} if (flag1 && flag2) returntrue; } returnfalse; }
intmaxProduct(vector<string>& words){ int max = 0; for (int i = 0; i < words.size(); ++i) { for (int j = i; j < words.size(); ++j) { int temp = words[i].size() * words[j].size(); if (!have_same_char(words[i], words[j]) && temp > max) { max = temp; } } } return max; } };
2.位运算
核心思想:
使用一个 int 来代指某个 word[i]:低 26 来代指字母 a-z 是否出现过,
然后对每个字符对对应的两个 int 值执行 & 操作(若两字符无重复字符,则结果为 0),并得出最终答案。
classSolution { public: intmax(int a, int b){ return a > b ? a : b; } intmaxProduct(vector<string>& words){ int len = words.size(); int *masks = newint[len]; int idx = 0; //1.对vector<string>数组进行预处理 对words中的每个string 中的每个char进行位运算 使其便于处理 for (string word : words) { int temp = 0; for (int i = 0; i < word.size(); ++i) temp |= (1 << word[i] - 'a'); masks[idx++] = temp; }
//2.对二进制数进行暴力遍历操作 int ans = 0; for (int i = 0; i < len; i++) { for (int j = 0; j < i; j++) { if ((masks[i] & masks[j]) == 0) ans = max(ans, words[i].length() * words[j].length());//不重复 } } return ans; } };
classSolution { public: intmax(int a, int b){ return a > b ? a : b; } intmaxProduct(vector<string>& words){ unordered_map<int, int> unmap; //1.对vector<string>数组进行预处理 对words中的每个string 中的每个char进行位运算 使其便于处理 for (string word : words) { int temp = 0; int len = word.size(); for (int i = 0; i < word.size(); ++i) temp |= (1 << (word[i] - 'a')); if (!unmap.count(temp) || (unmap[temp] < len)) unmap.insert(make_pair(temp, len)); }
//2.对二进制数进行暴力遍历操作 int ans = 0; for (auto a : unmap) { for (auto b : unmap) { if ((a.first & b.first) == 0) ans = max(ans, a.second * b.second); } } return ans; } };