- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0010-regular-expression-matching.rb
40 lines (36 loc) · 898 Bytes
/
0010-regular-expression-matching.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true
# 10. Regular Expression Matching
# https://leetcode.com/problems/regular-expression-matching
# @param {String} s
# @param {String} p
# @return {Boolean}
defis_match(s,p)
s=s.bytes
p=p.bytes
dp=Array.new(s.length + 1){Array.new(p.length + 1,false)}
m=s.length
n=p.length
dp[m][n]=true
m.downto(0)do |i|
(n - 1).downto(0)do |j|
first=i < m && (s[i] == p[j] || p[j] == 46)
dp[i][j]=ifp[j + 1] == 42
dp[i][j + 2] || (first && dp[i + 1][j])
else
first && dp[i + 1][j + 1]
end
end
end
dp[0][0]
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_is_match < Test::Unit::TestCase
deftest_
assert_equalfalse,is_match("aa","a")
assert_equaltrue,is_match("aa","a*")
assert_equaltrue,is_match("ab",".*")
end
end