File tree 4 files changed +48
-4
lines changed
4 files changed +48
-4
lines changed Original file line number Diff line number Diff line change @@ -627,6 +627,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
627
627
628
628
### Hard
629
629
630
- | Name | Link to LeetCode | Link to solution | Link to tests |
631
- | ------------------------------ | ------------------------------------------------------------------ | --------------------------------------------------- | --------------------------------------------------------- |
632
- | 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 ) |
630
+ | Name | Link to LeetCode | Link to solution | Link to tests |
631
+ | ------------------------------- | ------------------------------------------------------------------ | ---------------------------------------------------- | ---------------------------------------------------------- |
632
+ | 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 ) |
633
+ | 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 ) |
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'English'
5
5
::Gem ::Specification . new do |s |
6
6
s . required_ruby_version = '>= 3.0'
7
7
s . name = 'leetcode-ruby'
8
- s . version = '7.6.0. 1'
8
+ s . version = '7.6.1'
9
9
s . license = 'MIT'
10
10
s . files = ::Dir [ 'lib/**/*.rb' ] + %w[ README.md ]
11
11
s . executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ @mem = { }
4
+
5
+ # https://leetcode.com/problems/regular-expression-matching/
6
+ # @param {String} s
7
+ # @param {String} p
8
+ # @return {Boolean}
9
+ def is_match ( s , p )
10
+ s = s . bytes
11
+ p = p . bytes
12
+ dp = ::Array . new ( s . length + 1 ) { ::Array . new ( p . length + 1 , false ) }
13
+ m = s . length
14
+ n = p . length
15
+ dp [ m ] [ n ] = true
16
+
17
+ ( 0 ..m ) . reverse_each do |i |
18
+ ( n - 1 ) . downto ( 0 ) do |j |
19
+ first = i < m && ( s [ i ] == p [ j ] || p [ j ] == 46 )
20
+ dp [ i ] [ j ] =
21
+ if p [ j + 1 ] == 42
22
+ dp [ i ] [ j + 2 ] || ( first && dp [ i + 1 ] [ j ] )
23
+ else
24
+ first && dp [ i + 1 ] [ j + 1 ]
25
+ end
26
+ end
27
+ end
28
+
29
+ dp [ 0 ] [ 0 ]
30
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../test_helper'
4
+ require_relative '../../lib/hard/10_regular_expression_matching'
5
+ require 'minitest/autorun'
6
+
7
+ class RegularExpressionMatchingTest < ::Minitest ::Test
8
+ def test_default_one = assert ( !is_match ( 'aa' , 'a' ) )
9
+
10
+ def test_default_two = assert ( is_match ( 'aa' , 'a*' ) )
11
+
12
+ def test_default_three = assert ( is_match ( 'ab' , '.*' ) )
13
+ end
You can’t perform that action at this time.
0 commit comments