Skip to content

Commit 69f3478

Browse files
authored
Merge pull request #752 from fartem/318_Maximum_Product_of_Word_Lengths
2024-10-21 v. 6.8.5: added "318. Maximum Product of Word Lengths"
2 parents 41604b0 + dcd7906 commit 69f3478

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
550550
| 300. Longest Increasing Subsequence | [Link](https://leetcode.com/problems/longest-increasing-subsequence/) | [Link](./lib/medium/300_longest_increasing_subsequence.rb) | [Link](./test/medium/test_300_longest_increasing_subsequence.rb) |
551551
| 304. Range Sum Query 2D - Immutable | [Link](https://leetcode.com/problems/range-sum-query-2d-immutable/) | [Link](./lib/medium/304_range_sum_query_2d_immutable.rb) | [Link](./test/medium/test_304_range_sum_query_2d_immutable.rb) |
552552
| 316. Remove Duplicate Letters | [Link](https://leetcode.com/problems/remove-duplicate-letters/) | [Link](./lib/medium/316_remove_duplicate_letters.rb) | [Link](./test/medium/test_316_remove_duplicate_letters.rb) |
553+
| 318. Maximum Product of Word Lengths | [Link](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [Link](./lib/medium/318_maximum_product_of_word_lengths.rb) | [Link](./test/medium/test_318_maximum_product_of_word_lengths.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '6.8.4'
8+
s.version = '6.8.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/maximum-product-of-word-lengths/
4+
# @param {String[]} words
5+
# @return {Integer}
6+
def max_product318(words)
7+
result = 0
8+
letters = ::Hash.new { |hash, key| hash[key] = ::Array.new(26, 0) }
9+
words.each { |word| word.each_char { |char| letters[word][char.ord - 97] += 1 } }
10+
words.each_with_index do |word, i|
11+
w_letters = letters[word]
12+
words.each_with_index do |candidate, k|
13+
next if k == i
14+
15+
c_letters = letters[candidate]
16+
correct = true
17+
w_letters.each_with_index do |w_letter, m|
18+
next unless w_letter != 0 && c_letters[m] != 0
19+
20+
correct = false
21+
22+
break
23+
end
24+
25+
result = [result, word.length * candidate.length].max if correct
26+
end
27+
end
28+
29+
result
30+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/318_maximum_product_of_word_lengths'
5+
require 'minitest/autorun'
6+
7+
class MaximumProductOfWordLengthsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
16,
11+
max_product318(
12+
%w[abcw baz foo bar xtfn abcdef]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
4,
20+
max_product318(
21+
%w[a ab abc d cd bcd abcd]
22+
)
23+
)
24+
end
25+
26+
def test_default_three
27+
assert_equal(
28+
0,
29+
max_product318(
30+
%w[a aa aaa aaaa]
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)