Skip to content

Commit 9ae9998

Browse files
committed
2025-01-09 v. 7.8.2: added "890. Find and Replace Pattern"
1 parent 8255192 commit 9ae9998

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
634634
| 869. Reordered Power of 2 | [Link](https://leetcode.com/problems/reordered-power-of-2/) | [Link](./lib/medium/869_reordered_power_of_2.rb) | [Link](./test/medium/test_869_reordered_power_of_2.rb) |
635635
| 880. Decoded String at Index | [Link](https://leetcode.com/problems/decoded-string-at-index/) | [Link](./lib/medium/880_decoded_string_at_index.rb) | [Link](./test/medium/test_880_decoded_string_at_index.rb) |
636636
| 889. Construct Binary Tree from Preorder and Postorder Traversal | [Link](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/) | [Link](./lib/medium/889_construct_binary_tree_from_preorder_and_postorder_traversal.rb) | [Link](./test/medium/test_889_construct_binary_tree_from_preorder_and_postorder_traversal.rb) |
637+
| 890. Find and Replace Pattern | [Link](https://leetcode.com/problems/find-and-replace-pattern/) | [Link](./lib/medium/890_find_and_replace_pattern.rb) | [Link](./test/medium/test_890_find_and_replace_pattern.rb) |
637638

638639
### Hard
639640

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 = '7.8.1'
8+
s.version = '7.8.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/find-and-replace-pattern/
4+
# @param {String[]} words
5+
# @param {String} pattern
6+
# @return {String[]}
7+
def find_and_replace_pattern(words, pattern) = words.select { |word| are_match(word, pattern) }
8+
9+
private
10+
11+
# @param {String} word
12+
# @param {String} word
13+
# @return {Boolean}
14+
def are_match(word, pattern)
15+
values = {}
16+
(0...word.size).each do |i|
17+
w = word[i]
18+
p = pattern[i]
19+
20+
values[w] = p unless values.include?(w)
21+
22+
return false unless values[w] == p
23+
end
24+
25+
seen = ::Array.new(128, false)
26+
values.each_value do |value|
27+
return false if seen[value.ord]
28+
29+
seen[value.ord] = true
30+
end
31+
32+
true
33+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/890_find_and_replace_pattern'
5+
require 'minitest/autorun'
6+
7+
class FindAndReplacePatternTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
%w[mee aqq],
11+
find_and_replace_pattern(
12+
%w[abc deq mee aqq dkd ccc],
13+
'abb'
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
%w[a b c],
21+
find_and_replace_pattern(
22+
%w[a b c],
23+
'a'
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)