-
Notifications
You must be signed in to change notification settings - Fork 1
/
wildcard.go
92 lines (78 loc) · 1.39 KB
/
wildcard.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package wildcard
type Matcher struct {
S byte
M byte
}
func NewMatcher() *Matcher {
return &Matcher{
S: '?',
M: '*',
}
}
func (m *Matcher) isWildPattern(pattern string) bool {
for i := range pattern {
c := pattern[i]
if c == m.M {
return true
}
if c == m.S {
return true
}
}
return false
}
func (m *Matcher) Match(pattern string, s string) (bool, error) {
// Edge cases.
if pattern == string(m.M) {
return true, nil
}
if pattern == "" {
if s == "" {
return true, nil
}
return false, nil
}
// If pattern does not contain wildcard chars, just compare the strings
// to avoid extra memory allocation.
if !m.isWildPattern(pattern) {
return pattern == s, nil
}
// Initialize DP.
lp := len(pattern)
ls := len(s)
dp := make([][]bool, lp+1)
for i := 0; i < lp+1; i++ {
dp[i] = make([]bool, ls+1)
}
dp[0][0] = true
for i := 0; i < lp; i++ {
if pattern[i] == m.M {
dp[i+1][0] = dp[i][0]
} else {
dp[i+1][0] = false
}
}
for j := 0; j < ls; j++ {
dp[0][j+1] = false
}
// Start DP.
for i := 0; i < lp; i++ {
for j := 0; j < ls; j++ {
pc := pattern[i]
sc := s[j]
switch pattern[i] {
case m.M:
dp[i+1][j+1] = dp[i][j] || dp[i][j+1] || dp[i+1][j]
case m.S:
dp[i+1][j+1] = dp[i][j]
default:
if pc == sc {
dp[i+1][j+1] = dp[i][j]
} else {
dp[i+1][j+1] = false
}
}
}
}
return dp[lp][ls], nil
}