技巧为int为32位,小写单词字母只由26位(大小写的话就是int_64t),将每一个字母确定一个独立的bit位,通过 & 计算来判断是否重复即可
- int Init = []()
- {
- cin.tie(0) -> sync_with_stdio(false);
- return 0;
- }();
- class Solution {
- public:
- int maxProduct(vector
& words) - {
- int n = words.size();
- vector<int>cnt(n);
- int ans = 0;
- for(int i = 0;i < n;i++)
- {
- auto& s = words[i];
- int m = s.size();
- for(int j = 0; j < m; j++)
- cnt[i] |= (1 << (s[j] - 'a'));
- }
- for(int i = 0; i < n; i++)
- {
- for(int j = i + 1; j < n; j++)
- {
- if(!(cnt[i] & cnt[j]))
- ans = max(ans,(int)(words[i].size() * words[j].size()));
- }
- }
- return ans;
- }
- };