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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
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.6.7'
s.version = '7.6.8'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
39 changes: 39 additions & 0 deletions lib/hard/30_substring_with_concatenation_of_all_words.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions test/hard/test_30_substring_with_concatenation_of_all_words.rb
Original file line number Diff line number Diff line change
@@ -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
Loading