Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024-10-21 v. 6.8.5: added "318. Maximum Product of Word Lengths" #752

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading