Skip to content
Merged
Show file tree
Hide file tree
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
Expand Up @@ -634,6 +634,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |

### Hard

Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '7.8.1'
s.version = '7.8.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
33 changes: 33 additions & 0 deletions lib/medium/890_find_and_replace_pattern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# https://leetcode.com/problems/find-and-replace-pattern/
# @param {String[]} words
# @param {String} pattern
# @return {String[]}
def find_and_replace_pattern(words, pattern) = words.select { |word| are_match(word, pattern) }

private

# @param {String} word
# @param {String} word
# @return {Boolean}
def are_match(word, pattern)
values = {}
(0...word.size).each do |i|
w = word[i]
p = pattern[i]

values[w] = p unless values.include?(w)

return false unless values[w] == p
end

seen = ::Array.new(128, false)
values.each_value do |value|
return false if seen[value.ord]

seen[value.ord] = true
end

true
end
27 changes: 27 additions & 0 deletions test/medium/test_890_find_and_replace_pattern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/890_find_and_replace_pattern'
require 'minitest/autorun'

class FindAndReplacePatternTest < ::Minitest::Test
def test_default_one
assert_equal(
%w[mee aqq],
find_and_replace_pattern(
%w[abc deq mee aqq dkd ccc],
'abb'
)
)
end

def test_default_two
assert_equal(
%w[a b c],
find_and_replace_pattern(
%w[a b c],
'a'
)
)
end
end
Loading