Skip to content

2024-06-07 v. 5.8.4: added "2506. Count Pairs Of Similar Strings" #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2024
Merged
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
@@ -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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -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'
25 changes: 25 additions & 0 deletions lib/easy/2506_count_pairs_of_similar_strings.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/easy/test_2506_count_pairs_of_similar_strings.rb
Original file line number Diff line number Diff line change
@@ -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