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 e514ee6

Browse files
D. J.:
- Added the leetcode problem and solution for 318
1 parent a0bf3f7 commit e514ee6

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
- [295 Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/description/)
224224
- [300 Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/description/)
225225
- [309 Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
226+
- [318 Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/description/)
226227
- [322 Coin Change](https://leetcode.com/problems/coin-change/description/)
227228
- [337 House Robber III](https://leetcode.com/problems/house-robber-iii/description/)
228229
- [338 Counting Bits](https://leetcode.com/problems/counting-bits/description/)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def maxProduct(self, words: List[str]) -> int:
8+
"""
9+
Given a string array words, return the maximum value of
10+
length(word[i]) * length(word[j]) where the two words do not share common
11+
letters. If no such two words exist, return 0.
12+
"""
13+
n = len(words)
14+
masks = [0] * n
15+
16+
# Create bitmask for each word
17+
for i, word in enumerate(words):
18+
mask = 0
19+
for ch in word:
20+
mask |= 1 << (ord(ch) - ord("a"))
21+
masks[i] = mask
22+
23+
result = 0
24+
for i in range(n):
25+
for j in range(i + 1, n):
26+
# If no common letters, bitwise AND will be 0
27+
if masks[i] & masks[j] == 0:
28+
result = max(result, len(words[i]) * len(words[j]))
29+
return result
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._318_maximum_product_of_word_lengths import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["words", "expected"],
10+
argvalues=[
11+
(["abcw", "baz", "foo", "bar", "xtfn", "abcdef"], 16),
12+
(["a", "ab", "abc", "d", "cd", "bcd", "abcd"], 4),
13+
(["a", "aa", "aaa", "aaaa"], 0),
14+
],
15+
)
16+
def test_func(words: List[str], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
max_product = Solution().maxProduct(words)
19+
assert max_product == expected

0 commit comments

Comments
(0)

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