Skip to content

Commit 153552a

Browse files
committed
2024-06-07 v. 5.8.4: added "2506. Count Pairs Of Similar Strings"
1 parent f5a22d5 commit 153552a

4 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
444444
| 2490. Circular Sentence | [Link](https://leetcode.com/problems/circular-sentence/) | [Link](./lib/easy/2490_circular_sentence.rb) |
445445
| 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) |
446446
| 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) |
447+
| 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) |

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 = '5.8.3'
8+
s.version = '5.8.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
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+
require 'set'
4+
5+
# https://leetcode.com/problems/count-pairs-of-similar-strings/
6+
# @param {String[]} words
7+
# @return {Integer}
8+
def similar_pairs(words)
9+
map = {}
10+
result = 0
11+
12+
(0...words.length).each do |i|
13+
word = words[i]
14+
map[word] = word.chars.to_set unless map.key?(word)
15+
16+
((i + 1)...words.length).each do |j|
17+
compare = words[j]
18+
map[compare] = compare.chars.to_set unless map.key?(compare)
19+
20+
result += 1 if map[word] == map[compare]
21+
end
22+
end
23+
24+
result
25+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2506_count_pairs_of_similar_strings'
5+
require 'minitest/autorun'
6+
7+
class CountPairsOfSimilarStringsTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(2, similar_pairs(%w[aba aabb abcd bac aabc]))
10+
assert_equal(3, similar_pairs(%w[aabb ab ba]))
11+
assert_equal(0, similar_pairs(%w[nba cba dba]))
12+
end
13+
end

0 commit comments

Comments
 (0)