Skip to content

Commit ac27505

Browse files
committed
wright solution takes too long
1 parent ba3662d commit ac27505

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

py3/hard/is_match/is_match.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ def match(self, s, place):
2626
if self.any_quantity:
2727
if not self.next:
2828
return True # .* at the end matches everything
29-
try_this = self.match(s, place+1)
29+
try_this = self.next.match(s, place+1)
3030
if try_this:
3131
return True
32-
return self.next.match(s, place+1)
32+
try_this = self.next.match(s, place)
33+
if try_this:
34+
return True
35+
return self.match(s, place+1)
3336
if not self.next:
3437
return place+1 == len(s)
3538
return self.next.match(s, place+1)
@@ -45,7 +48,9 @@ def match(self, s, place):
4548
if not self.next:
4649
return self.match(s, place+1)
4750
try_this = self.next.match(s, place+1)
48-
51+
if try_this:
52+
return True
53+
try_this = self.next.match(s, place)
4954
if try_this:
5055
return True
5156
return self.match(s, place+1)
@@ -63,6 +68,8 @@ def isMatch(self, s, p):
6368
if not head:
6469
head = r
6570
tail = r
71+
if not head:
72+
return not s
6673
return head.match(s, 0)
6774

6875

py3/hard/is_match/test/test1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"input": "aa\na*\n",
2+
"input": "aa\na*",
33
"output": "True"
44
}

py3/hard/is_match/test/test7

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"input": "bbbba\n.*a*a",
3+
"output": "True"
4+
}

py3/hard/is_match/test/test8_long

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"input":"aaaaaaaaaaaaab\na*a*a*a*a*a*a*a*a*a*c",
3+
"output":"False"
4+
}

0 commit comments

Comments
 (0)