Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6bc3a80

Browse files
committed
feat: add solutions to lc problem: No.0318
No.0318.Maximum Product of Word Lengths
1 parent 6d150e7 commit 6bc3a80

File tree

6 files changed

+235
-15
lines changed

6 files changed

+235
-15
lines changed

‎solution/0300-0399/0318.Maximum Product of Word Lengths/README.md‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<strong>输出: </strong><code>0
2727
<strong>解释: </strong>不存在这样的两个单词。</code></pre>
2828

29-
3029
## 解法
3130

3231
<!-- 这里可写通用的实现逻辑 -->
@@ -38,15 +37,97 @@
3837
<!-- 这里可写当前语言的特殊实现逻辑 -->
3938

4039
```python
41-
40+
class Solution:
41+
def maxProduct(self, words: List[str]) -> int:
42+
n = len(words)
43+
masks = [0] * n
44+
for i, word in enumerate(words):
45+
for c in word:
46+
masks[i] |= (1 << (ord(c) - ord('a')))
47+
ans = 0
48+
for i in range(n - 1):
49+
for j in range(i + 1, n):
50+
if (masks[i] & masks[j]) == 0:
51+
ans = max(ans, len(words[i]) * len(words[j]))
52+
return ans
4253
```
4354

4455
### **Java**
4556

4657
<!-- 这里可写当前语言的特殊实现逻辑 -->
4758

4859
```java
60+
class Solution {
61+
public int maxProduct(String[] words) {
62+
int n = words.length;
63+
int[] masks = new int[n];
64+
for (int i = 0; i < n; ++i) {
65+
for (char c : words[i].toCharArray()) {
66+
masks[i] |= (1 << (c - 'a'));
67+
}
68+
}
69+
int ans = 0;
70+
for (int i = 0; i < n - 1; ++i) {
71+
for (int j = i + 1; j < n; ++j) {
72+
if ((masks[i] & masks[j]) == 0) {
73+
ans = Math.max(ans, words[i].length() * words[j].length());
74+
}
75+
}
76+
}
77+
return ans;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int maxProduct(vector<string>& words) {
88+
int n = words.size();
89+
vector<int> masks(n);
90+
for (int i = 0; i < n; ++i)
91+
for (char c : words[i])
92+
masks[i] |= (1 << (c - 'a'));
93+
int ans = 0;
94+
for (int i = 0; i < n - 1; ++i)
95+
for (int j = i + 1; j < n; ++j)
96+
if ((masks[i] & masks[j]) == 0)
97+
ans = max(ans, (int) (words[i].size() * words[j].size()));
98+
return ans;
99+
}
100+
};
101+
```
49102
103+
### **Go**
104+
105+
```go
106+
func maxProduct(words []string) int {
107+
n := len(words)
108+
masks := make([]int, n)
109+
for i, word := range words {
110+
for _, c := range word {
111+
masks[i] |= (1 << (c - 'a'))
112+
}
113+
}
114+
ans := 0
115+
for i := 0; i < n-1; i++ {
116+
for j := i + 1; j < n; j++ {
117+
if (masks[i] & masks[j]) == 0 {
118+
ans = max(ans, len(words[i])*len(words[j]))
119+
}
120+
}
121+
}
122+
return ans
123+
}
124+
125+
func max(a, b int) int {
126+
if a > b {
127+
return a
128+
}
129+
return b
130+
}
50131
```
51132

52133
### **...**

‎solution/0300-0399/0318.Maximum Product of Word Lengths/README_EN.md‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,102 @@
4040
<li><code>words[i]</code> consists only of lowercase English letters.</li>
4141
</ul>
4242

43-
4443
## Solutions
4544

4645
<!-- tabs:start -->
4746

4847
### **Python3**
4948

5049
```python
51-
50+
class Solution:
51+
def maxProduct(self, words: List[str]) -> int:
52+
n = len(words)
53+
masks = [0] * n
54+
for i, word in enumerate(words):
55+
for c in word:
56+
masks[i] |= (1 << (ord(c) - ord('a')))
57+
ans = 0
58+
for i in range(n - 1):
59+
for j in range(i + 1, n):
60+
if (masks[i] & masks[j]) == 0:
61+
ans = max(ans, len(words[i]) * len(words[j]))
62+
return ans
5263
```
5364

5465
### **Java**
5566

5667
```java
68+
class Solution {
69+
public int maxProduct(String[] words) {
70+
int n = words.length;
71+
int[] masks = new int[n];
72+
for (int i = 0; i < n; ++i) {
73+
for (char c : words[i].toCharArray()) {
74+
masks[i] |= (1 << (c - 'a'));
75+
}
76+
}
77+
int ans = 0;
78+
for (int i = 0; i < n - 1; ++i) {
79+
for (int j = i + 1; j < n; ++j) {
80+
if ((masks[i] & masks[j]) == 0) {
81+
ans = Math.max(ans, words[i].length() * words[j].length());
82+
}
83+
}
84+
}
85+
return ans;
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
int maxProduct(vector<string>& words) {
96+
int n = words.size();
97+
vector<int> masks(n);
98+
for (int i = 0; i < n; ++i)
99+
for (char c : words[i])
100+
masks[i] |= (1 << (c - 'a'));
101+
int ans = 0;
102+
for (int i = 0; i < n - 1; ++i)
103+
for (int j = i + 1; j < n; ++j)
104+
if ((masks[i] & masks[j]) == 0)
105+
ans = max(ans, (int) (words[i].size() * words[j].size()));
106+
return ans;
107+
}
108+
};
109+
```
57110
111+
### **Go**
112+
113+
```go
114+
func maxProduct(words []string) int {
115+
n := len(words)
116+
masks := make([]int, n)
117+
for i, word := range words {
118+
for _, c := range word {
119+
masks[i] |= (1 << (c - 'a'))
120+
}
121+
}
122+
ans := 0
123+
for i := 0; i < n-1; i++ {
124+
for j := i + 1; j < n; j++ {
125+
if (masks[i] & masks[j]) == 0 {
126+
ans = max(ans, len(words[i])*len(words[j]))
127+
}
128+
}
129+
}
130+
return ans
131+
}
132+
133+
func max(a, b int) int {
134+
if a > b {
135+
return a
136+
}
137+
return b
138+
}
58139
```
59140

60141
### **...**
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
22
public:
33
int maxProduct(vector<string>& words) {
4-
vector<int> v(words.size(), 0) ;
5-
for (int i = 0; i < words.size(); ++i)
6-
for (auto ch: words[i])
7-
v[i] |= 1 << (ch-'a') ;
8-
9-
int M = 0;
10-
for (int i = 0; i < words.size(); ++i)
11-
for (int j = i+1; j < words.size(); ++j)
12-
if ((v[i] & v[j]) == 0)
13-
M = max(M, (int)words[i].size() * (int)words[j].size());
14-
return M ;
4+
int n = words.size();
5+
vector<int> masks(n);
6+
for (int i = 0; i < n; ++i)
7+
for (char c : words[i])
8+
masks[i] |= (1 << (c - 'a'));
9+
int ans = 0;
10+
for (int i = 0; i < n - 1; ++i)
11+
for (int j = i + 1; j < n; ++j)
12+
if ((masks[i] & masks[j]) == 0)
13+
ans = max(ans, (int) (words[i].size() * words[j].size()));
14+
return ans;
1515
}
1616
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func maxProduct(words []string) int {
2+
n := len(words)
3+
masks := make([]int, n)
4+
for i, word := range words {
5+
for _, c := range word {
6+
masks[i] |= (1 << (c - 'a'))
7+
}
8+
}
9+
ans := 0
10+
for i := 0; i < n-1; i++ {
11+
for j := i + 1; j < n; j++ {
12+
if (masks[i] & masks[j]) == 0 {
13+
ans = max(ans, len(words[i])*len(words[j]))
14+
}
15+
}
16+
}
17+
return ans
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int maxProduct(String[] words) {
3+
int n = words.length;
4+
int[] masks = new int[n];
5+
for (int i = 0; i < n; ++i) {
6+
for (char c : words[i].toCharArray()) {
7+
masks[i] |= (1 << (c - 'a'));
8+
}
9+
}
10+
int ans = 0;
11+
for (int i = 0; i < n - 1; ++i) {
12+
for (int j = i + 1; j < n; ++j) {
13+
if ((masks[i] & masks[j]) == 0) {
14+
ans = Math.max(ans, words[i].length() * words[j].length());
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxProduct(self, words: List[str]) -> int:
3+
n = len(words)
4+
masks = [0] * n
5+
for i, word in enumerate(words):
6+
for c in word:
7+
masks[i] |= (1 << (ord(c) - ord('a')))
8+
ans = 0
9+
for i in range(n - 1):
10+
for j in range(i + 1, n):
11+
if (masks[i] & masks[j]) == 0:
12+
ans = max(ans, len(words[i]) * len(words[j]))
13+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /