Skip to content

Commit d9a8afe

Browse files
authored
2024-12-17 v. 7.3.7: added "648. Replace Words"
2 parents 8267421 + 9d6104c commit d9a8afe

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
601601
| 623. Add One Row to Tree | [Link](https://leetcode.com/problems/add-one-row-to-tree/) | [Link](./lib/medium/623_add_one_row_to_tree.rb) | [Link](./test/medium/test_623_add_one_row_to_tree.rb) |
602602
| 641. Design Circular Deque | [Link](https://leetcode.com/problems/design-circular-deque/) | [Link](./lib/medium/641_design_circular_deque.rb) | [Link](./test/medium/test_641_design_circular_deque.rb) |
603603
| 647. Palindromic Substrings | [Link](https://leetcode.com/problems/palindromic-substrings/) | [Link](./lib/medium/647_palindromic_substrings.rb) | [Link](./test/medium/test_647_palindromic_substrings.rb) |
604+
| 648. Replace Words | [Link](https://leetcode.com/problems/replace-words/) | [Link](./lib/medium/648_replace_words.rb) | [Link](./test/medium/test_648_replace_words.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 = '7.3.6'
8+
s.version = '7.3.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/648_replace_words.rb

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+
# https://leetcode.com/problems/replace-words/
4+
# @param {String[]} dictionary
5+
# @param {String} sentence
6+
# @return {String}
7+
def replace_words(dictionary, sentence)
8+
roots = ::Set.new(dictionary)
9+
words = sentence.split
10+
result = []
11+
words.each do |word|
12+
insert_word = word
13+
(1...word.size).each do |i|
14+
sub = word[0, i]
15+
16+
next unless roots.include?(sub)
17+
18+
insert_word = sub
19+
20+
break
21+
end
22+
23+
result << insert_word
24+
end
25+
26+
result.join(' ')
27+
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/648_replace_words'
5+
require 'minitest/autorun'
6+
7+
class ReplaceWordsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'the cat was rat by the bat',
11+
replace_words(
12+
%w[cat bat rat],
13+
'the cattle was rattled by the battery'
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
'a a b c',
21+
replace_words(
22+
%w[a b c],
23+
'aadsfasf absbs bbab cadsfafs'
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)