From d07801049d38ac76c5442e55b5549ecf48929e02 Mon Sep 17 00:00:00 2001 From: fartem Date: Thu, 26 Dec 2024 11:38:55 +0300 Subject: [PATCH] 2024-12-26 v. 7.5.0: added "720. Longest Word in Dictionary" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/720_longest_word_in_dictionary.rb | 13 ++++++++++ .../test_720_longest_word_in_dictionary.rb | 25 +++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/medium/720_longest_word_in_dictionary.rb create mode 100644 test/medium/test_720_longest_word_in_dictionary.rb diff --git a/README.md b/README.md index 1bce8690..faecd003 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 128c9822..d8504640 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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' diff --git a/lib/medium/720_longest_word_in_dictionary.rb b/lib/medium/720_longest_word_in_dictionary.rb new file mode 100644 index 00000000..6993f139 --- /dev/null +++ b/lib/medium/720_longest_word_in_dictionary.rb @@ -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 diff --git a/test/medium/test_720_longest_word_in_dictionary.rb b/test/medium/test_720_longest_word_in_dictionary.rb new file mode 100644 index 00000000..ac047eae --- /dev/null +++ b/test/medium/test_720_longest_word_in_dictionary.rb @@ -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