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 @@ -614,3 +614,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
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.4.9'
s.version = '7.5.0'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
13 changes: 13 additions & 0 deletions lib/medium/720_longest_word_in_dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'set'

# https://leetcode.com/problems/longest-word-in-dictionary/
# @param {String[]} words
# @return {String}
def longest_word(words)
found_words = { '' => true }
words.sort.each { |word| found_words[word] = true if found_words[word[...-1]] }

found_words.keys.max_by(&:size)
end
25 changes: 25 additions & 0 deletions test/medium/test_720_longest_word_in_dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/720_longest_word_in_dictionary'
require 'minitest/autorun'

class LongestWordInDictionaryTest < ::Minitest::Test
def test_default_one
assert_equal(
'world',
longest_word(
%w[w wo wor worl world]
)
)
end

def test_default_two
assert_equal(
'apple',
longest_word(
%w[a banana app appl ap apply apple]
)
)
end
end
Loading