diff --git a/README.md b/README.md index 50cb21fa..03d53340 100644 --- a/README.md +++ b/README.md @@ -726,6 +726,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1985. Find the Kth Largest Integer in the Array | [Link](https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/) | [Link](./lib/medium/1985_find_the_kth_largest_integer_in_the_array.rb) | [Link](./test/medium/test_1985_find_the_kth_largest_integer_in_the_array.rb) | | 1996. The Number of Weak Characters in the Game | [Link](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/) | [Link](./lib/medium/1996_the_number_of_weak_characters_in_the_game.rb) | [Link](./test/medium/test_1996_the_number_of_weak_characters_in_the_game.rb) | | 2007. Find Original Array From Doubled Array | [Link](https://leetcode.com/problems/find-original-array-from-doubled-array/) | [Link](./lib/medium/2007_find_original_array_from_doubled_array.rb) | [Link](./test/medium/test_2007_find_original_array_from_doubled_array.rb) | +| 2023. Number of Pairs of Strings With Concatenation Equal to Target | [Link](https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/) | [Link](./lib/medium/2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb) | [Link](./test/medium/test_2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb) | | 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 27dd03f7..a31f4302 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '8.9.2' + s.version = '8.9.3' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb b/lib/medium/2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb new file mode 100644 index 00000000..2ae56b55 --- /dev/null +++ b/lib/medium/2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/ +# @param {String[]} nums +# @param {String} target +# @return {Integer} +def num_of_pairs(nums, target) + count = 0 + freq = ::Hash.new(0) + + nums.each { |num| freq[num] += 1 } + + nums.each do |s| + next unless target.start_with?(s) + + remaining = target[s.size..] + + next unless freq.key?(remaining) + + count += freq[remaining] + count -= 1 if s == remaining + end + + count +end diff --git a/test/medium/test_2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb b/test/medium/test_2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb new file mode 100644 index 00000000..93a2c31f --- /dev/null +++ b/test/medium/test_2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/2023_number_of_pairs_of_strings_with_concatenation_equal_to_target' +require 'minitest/autorun' + +class NumberOfPairsOfStringsWithConcatenationEqualToTargetTest < ::Minitest::Test + def test_default_one + assert_equal( + 4, + num_of_pairs( + %w[777 7 77 77], + '7777' + ) + ) + end + + def test_default_two + assert_equal( + 2, + num_of_pairs( + %w[123 4 12 34], + '1234' + ) + ) + end + + def test_default_three + assert_equal( + 6, + num_of_pairs( + %w[1 1 1], + '11' + ) + ) + end +end