Skip to content

Commit

Permalink
Merge pull request #752 from fartem/318_Maximum_Product_of_Word_Lengths
Browse files Browse the repository at this point in the history
2024-10-21 v. 6.8.5: added "318. Maximum Product of Word Lengths"
  • Loading branch information
fartem authored Oct 21, 2024
2 parents 41604b0 + dcd7906 commit 69f3478
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '6.8.4'
s.version = '6.8.5'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
30 changes: 30 additions & 0 deletions lib/medium/318_maximum_product_of_word_lengths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# https://leetcode.com/problems/maximum-product-of-word-lengths/
# @param {String[]} words
# @return {Integer}
def max_product318(words)
result = 0
letters = ::Hash.new { |hash, key| hash[key] = ::Array.new(26, 0) }
words.each { |word| word.each_char { |char| letters[word][char.ord - 97] += 1 } }
words.each_with_index do |word, i|
w_letters = letters[word]
words.each_with_index do |candidate, k|
next if k == i

c_letters = letters[candidate]
correct = true
w_letters.each_with_index do |w_letter, m|
next unless w_letter != 0 && c_letters[m] != 0

correct = false

break
end

result = [result, word.length * candidate.length].max if correct
end
end

result
end
34 changes: 34 additions & 0 deletions test/medium/test_318_maximum_product_of_word_lengths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/318_maximum_product_of_word_lengths'
require 'minitest/autorun'

class MaximumProductOfWordLengthsTest < ::Minitest::Test
def test_default_one
assert_equal(
16,
max_product318(
%w[abcw baz foo bar xtfn abcdef]
)
)
end

def test_default_two
assert_equal(
4,
max_product318(
%w[a ab abc d cd bcd abcd]
)
)
end

def test_default_three
assert_equal(
0,
max_product318(
%w[a aa aaa aaaa]
)
)
end
end

0 comments on commit 69f3478

Please sign in to comment.