From 153552a9aee842d80037b04e6cba629ae0e2d00a Mon Sep 17 00:00:00 2001 From: fartem Date: Fri, 7 Jun 2024 09:16:47 +0300 Subject: [PATCH] 2024-06-07 v. 5.8.4: added "2506. Count Pairs Of Similar Strings" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- .../2506_count_pairs_of_similar_strings.rb | 25 +++++++++++++++++++ ...est_2506_count_pairs_of_similar_strings.rb | 13 ++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/easy/2506_count_pairs_of_similar_strings.rb create mode 100644 test/easy/test_2506_count_pairs_of_similar_strings.rb diff --git a/README.md b/README.md index 1baf5a8f..68a0f5b3 100644 --- a/README.md +++ b/README.md @@ -444,3 +444,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2490. Circular Sentence | [Link](https://leetcode.com/problems/circular-sentence/) | [Link](./lib/easy/2490_circular_sentence.rb) | | 2496. Maximum Value of a String in an Array | [Link](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/) | [Link](./lib/easy/2496_maximum_value_of_a_string_in_an_array.rb) | | 2500. Delete Greatest Value in Each Row | [Link](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [Link](./lib/easy/2500_delete_greatest_value_in_each_row.rb) | +| 2506. Count Pairs Of Similar Strings | [Link](https://leetcode.com/problems/count-pairs-of-similar-strings/) | [Link](./lib/easy/2506_count_pairs_of_similar_strings.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 2c7ac1a4..33d959c5 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 = '5.8.3' + s.version = '5.8.4' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/2506_count_pairs_of_similar_strings.rb b/lib/easy/2506_count_pairs_of_similar_strings.rb new file mode 100644 index 00000000..48b26535 --- /dev/null +++ b/lib/easy/2506_count_pairs_of_similar_strings.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'set' + +# https://leetcode.com/problems/count-pairs-of-similar-strings/ +# @param {String[]} words +# @return {Integer} +def similar_pairs(words) + map = {} + result = 0 + + (0...words.length).each do |i| + word = words[i] + map[word] = word.chars.to_set unless map.key?(word) + + ((i + 1)...words.length).each do |j| + compare = words[j] + map[compare] = compare.chars.to_set unless map.key?(compare) + + result += 1 if map[word] == map[compare] + end + end + + result +end diff --git a/test/easy/test_2506_count_pairs_of_similar_strings.rb b/test/easy/test_2506_count_pairs_of_similar_strings.rb new file mode 100644 index 00000000..1bfea4ed --- /dev/null +++ b/test/easy/test_2506_count_pairs_of_similar_strings.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/2506_count_pairs_of_similar_strings' +require 'minitest/autorun' + +class CountPairsOfSimilarStringsTest < ::Minitest::Test + def test_default + assert_equal(2, similar_pairs(%w[aba aabb abcd bac aabc])) + assert_equal(3, similar_pairs(%w[aabb ab ba])) + assert_equal(0, similar_pairs(%w[nba cba dba])) + end +end