Skip to content

Commit 7c991b8

Browse files
authored
2025-03-12 v. 8.9.3: added "2023. Number of Pairs of Strings With Concatenation Equal to Target"
2 parents ceb3da6 + 166cbf8 commit 7c991b8

4 files changed

+64
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
726726
| 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) |
727727
| 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) |
728728
| 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) |
729+
| 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) |
729730
| 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) |
730731
| 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) |
731732
| 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) |

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 = '8.9.2'
8+
s.version = '8.9.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/
4+
# @param {String[]} nums
5+
# @param {String} target
6+
# @return {Integer}
7+
def num_of_pairs(nums, target)
8+
count = 0
9+
freq = ::Hash.new(0)
10+
11+
nums.each { |num| freq[num] += 1 }
12+
13+
nums.each do |s|
14+
next unless target.start_with?(s)
15+
16+
remaining = target[s.size..]
17+
18+
next unless freq.key?(remaining)
19+
20+
count += freq[remaining]
21+
count -= 1 if s == remaining
22+
end
23+
24+
count
25+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/2023_number_of_pairs_of_strings_with_concatenation_equal_to_target'
5+
require 'minitest/autorun'
6+
7+
class NumberOfPairsOfStringsWithConcatenationEqualToTargetTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
4,
11+
num_of_pairs(
12+
%w[777 7 77 77],
13+
'7777'
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
2,
21+
num_of_pairs(
22+
%w[123 4 12 34],
23+
'1234'
24+
)
25+
)
26+
end
27+
28+
def test_default_three
29+
assert_equal(
30+
6,
31+
num_of_pairs(
32+
%w[1 1 1],
33+
'11'
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)