Skip to content
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 @@ -738,6 +738,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2121. Intervals Between Identical Elements | [Link](https://leetcode.com/problems/intervals-between-identical-elements/) | [Link](./lib/medium/2121_intervals_between_identical_elements.rb) | [Link](./test/medium/test_2121_intervals_between_identical_elements.rb) |
| 2125. Number of Laser Beams in a Bank | [Link](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/) | [Link](./lib/medium/2125_number_of_laser_beams_in_a_bank.rb) | [Link](./test/medium/test_2125_number_of_laser_beams_in_a_bank.rb) |
| 2130. Maximum Twin Sum of a Linked List | [Link](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [Link](./lib/medium/2130_maximum_twin_sum_of_a_linked_list.rb) | [Link](./test/medium/test_2130_maximum_twin_sum_of_a_linked_list.rb) |
| 2131. Longest Palindrome by Concatenating Two Letter Words | [Link](https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/) | [Link](./lib/medium/2131_longest_palindrome_by_concatenating_two_letter_words.rb) | [Link](./test/medium/test_2131_longest_palindrome_by_concatenating_two_letter_words.rb) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) |
Expand Down
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 = '9.0.3'
s.version = '9.0.4'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/
# @param {String[]} words
# @return {Integer}
def longest_palindrome2131(words)
freq = ::Hash.new(0)
words.each { |word| freq[word] += 1 }

total = 0
processed = ::Set.new

freq.each_key do |word|
rev = word.reverse

next if word == rev

next if processed.include?(word)

processed.add(word)
processed.add(rev)

current_count = freq[word]
rev_count = freq[rev]
min_pairs = [current_count, rev_count].min
total += min_pairs * 4
end

has_odd_count = false

freq.each do |word, count|
next unless word == word.reverse

pairs = count / 2
total += pairs * 4

has_odd_count ||= count.odd?
end

total += 2 if has_odd_count

total
end
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/2131_longest_palindrome_by_concatenating_two_letter_words'
require 'minitest/autorun'

class LongestPalindromeByConcatenatingTwoLetterWordsTest < ::Minitest::Test
def test_default_one
assert_equal(
6,
longest_palindrome2131(
%w[lc cl gg]
)
)
end

def test_default_two
assert_equal(
8,
longest_palindrome2131(
%w[ab ty yt lc cl ab]
)
)
end

def test_default_three
assert_equal(
2,
longest_palindrome2131(
%w[cc ll xx]
)
)
end
end