Skip to content

Commit 05e3adc

Browse files
authored
2025-01-13 v. 7.9.8: added "936. Stamping The Sequence"
2 parents 6d06dd4 + aa74270 commit 05e3adc

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
667667
| 458. Poor Pigs | [Link](https://leetcode.com/problems/poor-pigs/) | [Link](./lib/hard/458_poor_pigs.rb) | [Link](./test/hard/test_458_poor_pigs.rb) |
668668
| 871. Minimum Number of Refueling Stops | [Link](https://leetcode.com/problems/minimum-number-of-refueling-stops/) | [Link](./lib/hard/871_minimum_number_of_refueling_stops.rb) | [Link](./test/hard/test_871_minimum_number_of_refueling_stops.rb) |
669669
| 895. Maximum Frequency Stack | [Link](https://leetcode.com/problems/maximum-frequency-stack/) | [Link](./lib/hard/895_maximum_frequency_stack.rb) | [Link](./test/hard/test_895_maximum_frequency_stack.rb) |
670+
| 936. Stamping The Sequence | [Link](https://leetcode.com/problems/stamping-the-sequence/) | [Link](./lib/hard/936_stamping_the_sequence.rb) | [Link](./test/hard/test_936_stamping_the_sequence.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.9.7'
8+
s.version = '7.9.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: false
2+
3+
# https://leetcode.com/problems/stamping-the-sequence/
4+
# @param {String} stamp
5+
# @param {String} target
6+
# @return {Integer[]}
7+
def moves_to_stamp(stamp, target)
8+
stamp_wc = '?' * stamp.size
9+
a =
10+
(10 * target.size).times.each_with_object([]) do |_j, ans|
11+
break ans unless (0..target.size - stamp.size).reduce(false) do |r, i|
12+
str = target[i, stamp.size]
13+
if !str.all_wild? && stamp.match_wild?(str)
14+
ans << i
15+
target[i, stamp.size] = stamp_wc
16+
17+
true
18+
else
19+
r
20+
end
21+
end
22+
end
23+
24+
target.all_wild? ? a.reverse : []
25+
end
26+
27+
# String expansion
28+
class String
29+
# Check is wild
30+
def wild? = eql?('?')
31+
32+
# Check is match
33+
def match_wild?(b)
34+
return false unless size == b.size
35+
36+
chars.zip(b.chars).all? { |cp| cp.first == cp.last || cp.any?(&:wild?) }
37+
end
38+
39+
# Check is all wild
40+
def all_wild? = chars.all?(&:wild?)
41+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: false
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/hard/936_stamping_the_sequence'
5+
require 'minitest/autorun'
6+
7+
class StampingTheSequenceTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[0, 2],
11+
moves_to_stamp(
12+
'abc',
13+
'ababc'
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
[0, 3, 1],
21+
moves_to_stamp(
22+
'abca',
23+
'aabcaca'
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)