-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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"
- Loading branch information
Showing
4 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |