-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregular_expression_matching.go
100 lines (93 loc) · 1.64 KB
/
regular_expression_matching.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
93
94
95
96
97
98
99
100
// Package regular_expression_matching
//
// Given an input string s and a pattern p,
// implement regular expression matching
// with support for '.' and '*' where:
//
// '.' Matches any single character.
//
// '*' Matches zero or more of the preceding element.
//
// The matching should cover the entire input string (not partial).
//
// Constraints:
//
// 1 <= s.length <= 20
//
// 1 <= p.length <= 20
//
// s contains only lowercase English letters.
//
// p contains only lowercase English letters, '.', and '*'.
//
// It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
package regular_expression_matching
func isMatch(s string, p string) bool {
if s == p {
return true
}
lenS := len(s)
lenP := len(p)
if lenP == 0 {
return lenS == 0
}
if lenP == 1 {
if lenS != 1 {
return false
}
if p == s {
return true
}
if p == "." {
return true
}
return false
}
if lenP == 2 {
if p[1] != '*' {
if len(s) < 2 {
return false
}
return isMatch(s[0:1], p[0:1]) && isMatch(s[1:], p[1:])
}
if lenS == 0 {
return true
}
if p[0] == '.' {
return true
}
if p[0] != s[0] {
return false
}
if lenS == 1 {
return true
}
for i := 1; i < len(s); i++ {
if s[i] != s[0] {
return false
}
}
return true
}
pChunkR := 1
if p[1] == '*' {
pChunkR = 2
}
pChunk := p[0:pChunkR]
for i := lenS; i >= 0; i-- {
var sChunk string
if i == lenS {
sChunk = s[0:]
} else {
sChunk = s[0:i]
}
if isMatch(sChunk, pChunk) {
sChunk := s[i:]
pChunk := p[pChunkR:]
if isMatch(sChunk, pChunk) {
return true
}
}
}
return false
}