-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwildcard_pattern_matching.rb
More file actions
55 lines (43 loc) · 1.26 KB
/
wildcard_pattern_matching.rb
File metadata and controls
55 lines (43 loc) · 1.26 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'minitest/autorun'
class TestWildcardPatternMatching < Minitest::Test
def test_positive_wilcard_pattern_matching
str = "abcde"
pat = "a?c*"
assert_equal true, wilcard_pattern_matching(str, pat), "For #{str} and #{pat}"
str = "a cde"
pat = "a?c*"
assert_equal true, wilcard_pattern_matching(str, pat), "For #{str} and #{pat}"
str = "baaabab"
pat = "*****ba*****ab"
assert_equal true, wilcard_pattern_matching(str, pat), "For #{str} and #{pat}"
str = "abc"
pat = "*"
assert_equal true, wilcard_pattern_matching(str, pat), "For #{str} and #{pat}"
end
def test_negative_wilcard_pattern_matching
str = "baaabab"
pat = "a*ab"
assert_equal nil, wilcard_pattern_matching(str, pat), "For #{str} and #{pat}"
end
end
def wilcard_pattern_matching(str, pat)
str = str.chars
pat = pat.gsub(/\*+/, "*").chars
strl = str.length
patl = pat.length
dp = Array.new(strl+1) { Array.new(patl+1) }
if patl > 0 && pat[0] == "*"
dp[0][1] = true
end
dp[0][0] = true
1.upto(strl) do |i|
1.upto(patl) do |j|
if str[i-1] == pat[j-1] || pat[j-1] == "?"
dp[i][j] = dp[i-1][j-1]
elsif pat[j-1] == "*"
dp[i][j] = dp[i-1][j] || dp[i][j-1]
end
end
end
dp[strl][patl]
end