Skip to content

Commit 53067a9

Browse files
authored
2025-03-20 v. 9.0.4: added "2131. Longest Palindrome by Concatenating Two Letter Words"
2 parents 681bdee + 038a95f commit 53067a9

4 files changed

+79
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
738738
| 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) |
739739
| 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) |
740740
| 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) |
741+
| 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) |
741742
| 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) |
742743
| 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) |
743744
| 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) |

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 = '9.0.3'
8+
s.version = '9.0.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/
4+
# @param {String[]} words
5+
# @return {Integer}
6+
def longest_palindrome2131(words)
7+
freq = ::Hash.new(0)
8+
words.each { |word| freq[word] += 1 }
9+
10+
total = 0
11+
processed = ::Set.new
12+
13+
freq.each_key do |word|
14+
rev = word.reverse
15+
16+
next if word == rev
17+
18+
next if processed.include?(word)
19+
20+
processed.add(word)
21+
processed.add(rev)
22+
23+
current_count = freq[word]
24+
rev_count = freq[rev]
25+
min_pairs = [current_count, rev_count].min
26+
total += min_pairs * 4
27+
end
28+
29+
has_odd_count = false
30+
31+
freq.each do |word, count|
32+
next unless word == word.reverse
33+
34+
pairs = count / 2
35+
total += pairs * 4
36+
37+
has_odd_count ||= count.odd?
38+
end
39+
40+
total += 2 if has_odd_count
41+
42+
total
43+
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/2131_longest_palindrome_by_concatenating_two_letter_words'
5+
require 'minitest/autorun'
6+
7+
class LongestPalindromeByConcatenatingTwoLetterWordsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
6,
11+
longest_palindrome2131(
12+
%w[lc cl gg]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
8,
20+
longest_palindrome2131(
21+
%w[ab ty yt lc cl ab]
22+
)
23+
)
24+
end
25+
26+
def test_default_three
27+
assert_equal(
28+
2,
29+
longest_palindrome2131(
30+
%w[cc ll xx]
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)