Skip to content

Commit 3bb70a9

Browse files
authored
2024-12-26 v. 7.5.0: added "720. Longest Word in Dictionary"
2 parents 94ffc5a + d078010 commit 3bb70a9

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
614614
| 701. Insert into a Binary Search Tree | [Link](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Link](./lib/medium/701_insert_into_a_binary_search_tree.rb) | [Link](./test/medium/test_701_insert_into_a_binary_search_tree.rb) |
615615
| 707. Design Linked List | [Link](https://leetcode.com/problems/design-linked-list/) | [Link](./lib/medium/707_design_linked_list.rb) | [Link](./test/medium/test_707_design_linked_list.rb) |
616616
| 713. Subarray Product Less Than K | [Link](https://leetcode.com/problems/subarray-product-less-than-k/) | [Link](./lib/medium/713_subarray_product_less_than_k.rb) | [Link](./test/medium/test_713_subarray_product_less_than_k.rb) |
617+
| 720. Longest Word in Dictionary | [Link](https://leetcode.com/problems/longest-word-in-dictionary/) | [Link](./lib/medium/720_longest_word_in_dictionary.rb) | [Link](./test/medium/test_720_longest_word_in_dictionary.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.4.9'
8+
s.version = '7.5.0'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
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 'set'
4+
5+
# https://leetcode.com/problems/longest-word-in-dictionary/
6+
# @param {String[]} words
7+
# @return {String}
8+
def longest_word(words)
9+
found_words = { '' => true }
10+
words.sort.each { |word| found_words[word] = true if found_words[word[...-1]] }
11+
12+
found_words.keys.max_by(&:size)
13+
end
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_relative '../test_helper'
4+
require_relative '../../lib/medium/720_longest_word_in_dictionary'
5+
require 'minitest/autorun'
6+
7+
class LongestWordInDictionaryTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'world',
11+
longest_word(
12+
%w[w wo wor worl world]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
'apple',
20+
longest_word(
21+
%w[a banana app appl ap apply apple]
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)