-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.正则表达式匹配.js
More file actions
167 lines (166 loc) · 4.15 KB
/
Copy path10.正则表达式匹配.js
File metadata and controls
167 lines (166 loc) · 4.15 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* @lc app=leetcode.cn id=10 lang=javascript
*
* [10] 正则表达式匹配
*
* https://leetcode-cn.com/problems/regular-expression-matching/description/
*
* algorithms
* Hard (20.82%)
* Total Accepted: 10.2K
* Total Submissions: 48.1K
* Testcase Example: '"aa"\n"a"'
*
* 给定一个字符串 (s) 和一个字符模式 (p)。实现支持 '.' 和 '*' 的正则表达式匹配。
*
* '.' 匹配任意单个字符。
* '*' 匹配零个或多个前面的元素。
*
*
* 匹配应该覆盖整个字符串 (s) ,而不是部分字符串。
*
* 说明:
*
*
* s 可能为空,且只包含从 a-z 的小写字母。
* p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *。
*
*
* 示例 1:
*
* 输入:
* s = "aa"
* p = "a"
* 输出: false
* 解释: "a" 无法匹配 "aa" 整个字符串。
*
*
* 示例 2:
*
* 输入:
* s = "aa"
* p = "a*"
* 输出: true
* 解释: '*' 代表可匹配零个或多个前面的元素, 即可以匹配 'a' 。因此, 重复 'a' 一次, 字符串可变为 "aa"。
*
*
* 示例 3:
*
* 输入:
* s = "ab"
* p = ".*"
* 输出: true
* 解释: ".*" 表示可匹配零个或多个('*')任意字符('.')。
*
*
* 示例 4:
*
* 输入:
* s = "aab"
* p = "c*a*b"
* 输出: true
* 解释: 'c' 可以不被重复, 'a' 可以被重复一次。因此可以匹配字符串 "aab"。
*
*
* 示例 5:
*
* 输入:
* s = "mississippi"
* p = "mis*is*p*."
* 输出: false
*
*/
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function (s, p) {
let index = 0;
let stars = [];
let indexS = 0;
let isStar = false;
while (indexS >= 0 && indexS < s.length) {
const charS = s[indexS];
const regexChar = p[index]
if (regexChar == "*") {
const starEnd = stars[stars.length - 1];
if (!starEnd || (starEnd && starEnd.index !== index)) {
stars.push({ index, passCount: 0, waitPassCount: 0, indexS })
++index;
continue
}
if (isStar) {
--starEnd.waitPassCount
}
if (!starEnd.waitPassCount) {
++index;
isStar = false;
} else {
--index;
}
continue
} else if (p[index + 1] === "*") {
const starEnd = stars[stars.length - 1];
if (!starEnd || (starEnd && starEnd.index != (index + 1))) {
stars.push({ index: index + 1, passCount: 0, waitPassCount: 0, indexS })
++index;
continue
}
}
if (regexChar === ".") {
++index
++indexS
continue;
}
if (regexChar === charS) {
++index;
++indexS;
continue;
}
if (isStar) {
stars.pop()
if (stars.length) {
const starEnd = stars[stars.length - 1];
++starEnd.passCount;
starEnd.waitPassCount = starEnd.passCount;
index = starEnd.index - 1;
indexS = starEnd.indexS;
isStar = true;
continue;
} else {
index = -1;
indexS = -1;
}
} else {
if (stars.length) {
const starEnd = stars[stars.length - 1];
starEnd.passCount = starEnd.passCount + 1;
starEnd.waitPassCount = starEnd.passCount;
index = starEnd.index - 1;
indexS = starEnd.indexS;
isStar = true;
continue;
} else {
index = -1;
indexS = -1;
}
}
}
if (indexS === s.length) {
while (index < p.length) {
const charP = p[index];
if (charP === "*") {
++index;
continue;
}
if (p[index + 1] === "*") {
index += 2;
continue
}
return false
}
return true
}
return false;
};