Skip to content

Commit 8989189

Browse files
authored
2025-01-03 v. 7.6.8: added "30. Substring with Concatenation of All Words"
2 parents df7894e + 9d6fd3f commit 8989189

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,10 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
631631

632632
### Hard
633633

634-
| Name | Link to LeetCode | Link to solution | Link to tests |
635-
| ------------------------------- | ------------------------------------------------------------------ | ---------------------------------------------------- | ---------------------------------------------------------- |
636-
| 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) |
637-
| 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) |
638-
| 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) |
639-
| 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) |
634+
| Name | Link to LeetCode | Link to solution | Link to tests |
635+
| --------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------ |
636+
| 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) |
637+
| 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) |
638+
| 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) |
639+
| 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) |
640+
| 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) |

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.6.7'
8+
s.version = '7.6.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/substring-with-concatenation-of-all-words/
4+
# @param {String} s
5+
# @param {String[]} words
6+
# @return {Integer[]}
7+
def find_substring(s, words)
8+
@word_count = words.tally
9+
@k = words.size
10+
@word_size = words[0].size
11+
@substring_size = @word_size * @k
12+
13+
result = []
14+
(0..(s.size - @substring_size)).each do |i|
15+
result << i if check_for_find_substring(i, s)
16+
end
17+
18+
result
19+
end
20+
21+
private
22+
23+
# @param {Integer} i
24+
# @param {String} s
25+
# @return {Boolean}
26+
def check_for_find_substring(i, s)
27+
rem = @word_count.clone
28+
words_used = 0
29+
(i...(i + @substring_size)).step(@word_size).each do |j|
30+
sub = s[j...(j + @word_size)]
31+
32+
break if rem.fetch(sub, 0).zero?
33+
34+
rem[sub] = rem[sub] - 1
35+
words_used += 1
36+
end
37+
38+
words_used == @k
39+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/hard/30_substring_with_concatenation_of_all_words'
5+
require 'minitest/autorun'
6+
7+
class SubstringWithConcatenationOfAllWordsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[0, 9],
11+
find_substring(
12+
'barfoothefoobarman',
13+
%w[foo bar]
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
[],
21+
find_substring(
22+
'wordgoodgoodgoodbestword',
23+
%w[word good best word]
24+
)
25+
)
26+
end
27+
28+
def test_default_three
29+
assert_equal(
30+
[6, 9, 12],
31+
find_substring(
32+
'barfoofoobarthefoobarman',
33+
%w[bar foo the]
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)