Skip to content

Commit 62d43c6

Browse files
committed
refactored is_match, removed binaries
1 parent ff513a3 commit 62d43c6

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed
Binary file not shown.
-1.59 KB
Binary file not shown.

py3/hard/is_match/is_match.py

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
11
class Solution(object):
2-
def fit(self, s, re_piece, offset, is_next_star):
3-
for i, c in enumerate(re_piece):
4-
if not (
5-
c == "." or
6-
s[i + offset] == "." or
7-
c == s[i + offset] or
8-
(i == len(re_piece) - 1 and is_next_star)
9-
):
2+
class Rune:
3+
def __new__(cls, c, prev):
4+
if c == "*":
5+
prev.any_quantity = True
6+
return prev
7+
return super(Solution.Rune, cls).__new__(cls)
8+
9+
def __init__(self, c, prev):
10+
if c != "*":
11+
self.c = c
12+
self.prev = prev
13+
self.any = c == "."
14+
self.next = None
15+
self.any_quantity = False
16+
if prev:
17+
prev.next = self
18+
19+
def match(self, s, place):
20+
if place >= len(s):
21+
if self.any_quantity:
22+
return self.next is None
1023
return False
11-
return True
12-
13-
def check_match(self, s, rg, rg_i, offset=0):
14-
if rg_i == len(rg):
15-
return len(s) == offset
16-
while offset < len(s):
17-
if self.fit(s, rg[rg_i][0], offset, rg_i + 1 < len(rg)):
18-
offset += max(len(rg[rg_i][0]), 1)
19-
return self.check_match(s, rg, rg_i+1, offset)
20-
elif offset + 1 < len(s) and s[offset+1] == rg[rg_i][1]:
21-
offset += 1
22-
else:
24+
25+
if self.any:
26+
if not self.next:
27+
return True
28+
return self.next.match(s, place+1)
29+
30+
if self.c != s[place]:
31+
if self.any_quantity:
32+
if not self.next:
33+
return False
34+
return self.next.match(s, place)
2335
return False
24-
if offset == len(s):
25-
return False
2636

27-
def isMatch(self, s, p):
28-
rg = []
29-
p_sp = p.split("*")
30-
for i, r in enumerate(p_sp):
31-
rg.append([r, p_sp[i-1][len(p_sp[i-1])-1] if i else "-"])
37+
if not self.next:
38+
return True
39+
40+
if self.any_quantity:
41+
try_this = self.next.match(s, place+1)
3242

33-
return self.check_match(s, rg, 0)
43+
if try_this:
44+
return True
45+
return self.match(s, place+1)
46+
return self.next.match(s, place+1)
47+
48+
def isMatch(self, s, p):
49+
head = None
50+
tail = None
51+
for c in p:
52+
r = self.Rune(c, tail)
53+
if not head:
54+
head = r
55+
tail = r
56+
return head.match(s, 0)
3457

3558

3659
def main(a, b):
-3.37 KB
Binary file not shown.
-669 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)