From 9d6fd3f4c02ee1715ae720988e92d02443ff1ad4 Mon Sep 17 00:00:00 2001 From: fartem Date: Fri, 3 Jan 2025 01:12:21 +0300 Subject: [PATCH] 2025-01-03 v. 7.6.8: added "30. Substring with Concatenation of All Words" --- README.md | 13 ++++--- leetcode-ruby.gemspec | 2 +- ...bstring_with_concatenation_of_all_words.rb | 39 +++++++++++++++++++ ...bstring_with_concatenation_of_all_words.rb | 37 ++++++++++++++++++ 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 lib/hard/30_substring_with_concatenation_of_all_words.rb create mode 100644 test/hard/test_30_substring_with_concatenation_of_all_words.rb diff --git a/README.md b/README.md index 93daa07e..7f48107b 100644 --- a/README.md +++ b/README.md @@ -631,9 +631,10 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). ### Hard -| Name | Link to LeetCode | Link to solution | Link to tests | -| ------------------------------- | ------------------------------------------------------------------ | ---------------------------------------------------- | ---------------------------------------------------------- | -| 4. Median of Two Sorted Arrays | [Link](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Link](./lib/hard/4_median_of_two_sorted_arrays.rb) | [Link](./test/hard/test_4_median_of_two_sorted_arrays.rb) | -| 10. Regular Expression Matching | [Link](https://leetcode.com/problems/regular-expression-matching/) | [Link](./lib/hard/10_regular_expression_matching.rb) | [Link](./test/hard/test_10_regular_expression_matching.rb) | -| 23. Merge k Sorted Lists | [Link](https://leetcode.com/problems/merge-k-sorted-lists/) | [Link](./lib/hard/23_merge_k_sorted_lists.rb) | [Link](./test/hard/test_23_merge_k_sorted_lists.rb) | -| 25. Reverse Nodes in k-Group | [Link](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Link](./lib/hard/25_reverse_nodes_in_k_group.rb) | [Link](./test/hard/test_25_reverse_nodes_in_k_group.rb) | +| Name | Link to LeetCode | Link to solution | Link to tests | +| --------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------ | +| 4. Median of Two Sorted Arrays | [Link](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Link](./lib/hard/4_median_of_two_sorted_arrays.rb) | [Link](./test/hard/test_4_median_of_two_sorted_arrays.rb) | +| 10. Regular Expression Matching | [Link](https://leetcode.com/problems/regular-expression-matching/) | [Link](./lib/hard/10_regular_expression_matching.rb) | [Link](./test/hard/test_10_regular_expression_matching.rb) | +| 23. Merge k Sorted Lists | [Link](https://leetcode.com/problems/merge-k-sorted-lists/) | [Link](./lib/hard/23_merge_k_sorted_lists.rb) | [Link](./test/hard/test_23_merge_k_sorted_lists.rb) | +| 25. Reverse Nodes in k-Group | [Link](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Link](./lib/hard/25_reverse_nodes_in_k_group.rb) | [Link](./test/hard/test_25_reverse_nodes_in_k_group.rb) | +| 30. Substring with Concatenation of All Words | [Link](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [Link](./lib/hard/30_substring_with_concatenation_of_all_words.rb) | [Link](./test/hard/test_30_substring_with_concatenation_of_all_words.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 965adba9..78883221 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.6.7' + s.version = '7.6.8' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/hard/30_substring_with_concatenation_of_all_words.rb b/lib/hard/30_substring_with_concatenation_of_all_words.rb new file mode 100644 index 00000000..d86c0be1 --- /dev/null +++ b/lib/hard/30_substring_with_concatenation_of_all_words.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/substring-with-concatenation-of-all-words/ +# @param {String} s +# @param {String[]} words +# @return {Integer[]} +def find_substring(s, words) + @word_count = words.tally + @k = words.size + @word_size = words[0].size + @substring_size = @word_size * @k + + result = [] + (0..(s.size - @substring_size)).each do |i| + result << i if check_for_find_substring(i, s) + end + + result +end + +private + +# @param {Integer} i +# @param {String} s +# @return {Boolean} +def check_for_find_substring(i, s) + rem = @word_count.clone + words_used = 0 + (i...(i + @substring_size)).step(@word_size).each do |j| + sub = s[j...(j + @word_size)] + + break if rem.fetch(sub, 0).zero? + + rem[sub] = rem[sub] - 1 + words_used += 1 + end + + words_used == @k +end diff --git a/test/hard/test_30_substring_with_concatenation_of_all_words.rb b/test/hard/test_30_substring_with_concatenation_of_all_words.rb new file mode 100644 index 00000000..e57921b5 --- /dev/null +++ b/test/hard/test_30_substring_with_concatenation_of_all_words.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/hard/30_substring_with_concatenation_of_all_words' +require 'minitest/autorun' + +class SubstringWithConcatenationOfAllWordsTest < ::Minitest::Test + def test_default_one + assert_equal( + [0, 9], + find_substring( + 'barfoothefoobarman', + %w[foo bar] + ) + ) + end + + def test_default_two + assert_equal( + [], + find_substring( + 'wordgoodgoodgoodbestword', + %w[word good best word] + ) + ) + end + + def test_default_three + assert_equal( + [6, 9, 12], + find_substring( + 'barfoofoobarthefoobarman', + %w[bar foo the] + ) + ) + end +end