Skip to content

Commit 918a893

Browse files
committed
maximum_product_of_word_lengths
1 parent 8ad8ab5 commit 918a893

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
230230
#### [313. Super Ugly Number](https://github.com/hitzzc/go-leetcode/tree/master/super_ugly_number)
231231
#### [315. Count of Smaller Numbers After Self](https://github.com/hitzzc/go-leetcode/tree/master/count_of_smaller_numbers_after_self)
232232
#### [316. Remove Duplicate Letters](https://github.com/hitzzc/go-leetcode/tree/master/remove_duplicate_letters)
233+
#### [318. Maximum Product of Word Lengths ](https://github.com/hitzzc/go-leetcode/tree/master/maximum_product_of_word_lengths)
233234

234235

235236

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<string>& words) {
4+
vector<int> helper(words.size(), 0);
5+
for (int i = 0; i < words.size(); ++i) {
6+
for (auto& ch: words[i]) {
7+
helper[i] |= 1 << (ch-'a');
8+
}
9+
}
10+
int ret = 0;
11+
for (int i = 0; i < helper.size(); ++i) {
12+
for (int j = i+1; j < helper.size(); ++j) {
13+
if (helper[i] & helper[j]) continue;
14+
if (words[i].size() * words[j].size() > ret) ret = words[i].size() * words[j].size();
15+
}
16+
}
17+
return ret;
18+
}
19+
};

0 commit comments

Comments
 (0)