Skip to content

Commit a21cc17

Browse files
authored
Merge pull request #795 from fartem/524-Longest-Word-in-Dictionary-through-Deleting
2024-12-06 v. 7.2.4: added "524. Longest Word in Dictionary through Deleting"
2 parents 2c8883a + 610fd97 commit a21cc17

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
589589
| 515. Find Largest Value in Each Tree Row | [Link](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | [Link](./lib/medium/515_find_largest_value_in_each_tree_row.rb) | [Link](./test/medium/test_515_find_largest_value_in_each_tree_row.rb) |
590590
| 516. Longest Palindromic Subsequence | [Link](https://leetcode.com/problems/longest-palindromic-subsequence/) | [Link](./lib/medium/516_longest_palindromic_subsequence.rb) | [Link](./test/medium/test_516_longest_palindromic_subsequence.rb) |
591591
| 523. Continuous Subarray Sum | [Link](https://leetcode.com/problems/continuous-subarray-sum/) | [Link](./lib/medium/523_continuous_subarray_sum.rb) | [Link](./test/medium/test_523_continuous_subarray_sum.rb) |
592+
| 524. Longest Word in Dictionary through Deleting | [Link](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/) | [Link](./lib/medium/524_longest_word_in_dictionary_through_deleting.rb) | [Link](./test/medium/test_524_longest_word_in_dictionary_through_deleting.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.2.3'
8+
s.version = '7.2.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/
4+
# @param {String} s
5+
# @param {String[]} dictionary
6+
# @return {String}
7+
def find_longest_word(s, dictionary)
8+
result = ''
9+
dictionary.each do |word|
10+
next unless is_valid_word_from_dictionary(s, word)
11+
12+
r_s = result.size
13+
w_s = word.size
14+
15+
result = word if r_s == w_s && result > word || w_s > r_s
16+
end
17+
18+
result
19+
end
20+
21+
private
22+
23+
# @param {String} s
24+
# @param {String} word
25+
# @return {Boolean}
26+
def is_valid_word_from_dictionary(s, word)
27+
s_p = 0
28+
w_p = 0
29+
while s_p < s.size && w_p < word.size
30+
w_p += s[s_p] == word[w_p] ? 1 : 0
31+
s_p += 1
32+
end
33+
34+
w_p == word.size
35+
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/524_longest_word_in_dictionary_through_deleting'
5+
require 'minitest/autorun'
6+
7+
class LongestWordInDictionaryThroughDeletingTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'apple',
11+
find_longest_word(
12+
'abpcplea',
13+
%w[ale apple monkey plea]
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
'a',
21+
find_longest_word(
22+
'abpcplea',
23+
%w[a b c]
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)