Skip to content

Commit 6d8c226

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 10
1 parent 4addbcd commit 6d8c226

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
- [7 Reverse Integer](https://leetcode.com/problems/reverse-integer/description/)
7474
- [8 String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/description/)
7575
- [9 Palindrome Number](https://leetcode.com/problems/palindrome-number/description/)
76+
- [10 Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/description/)
7677
- [11 Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/)
7778
- [12 Integer to Roman](https://leetcode.com/problems/integer-to-roman/description/)
7879
- [13 Roman to Integer](https://leetcode.com/problems/roman-to-integer/description/)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def isMatch(self, s: str, p: str) -> bool:
5+
"""
6+
Given an input string s and a pattern p, implement regular expression matching
7+
with support for '.' and '*' where:
8+
- '.' Matches any single characters.
9+
- '*' Matches zero or more of the preceding element.
10+
11+
The matching should cover the entire input string (not partial).
12+
"""
13+
# Time Complexity: O(n²)
14+
# Space Complexity: O(n²)
15+
dp = {}
16+
17+
def dfs(i: int, j: int) -> bool:
18+
if i >= len(s) and j >= len(p):
19+
return True
20+
if j >= len(p):
21+
return False
22+
if (i, j) in dp:
23+
return dp[(i, j)]
24+
match = i < len(s) and (s[i] == p[j] or p[j] == ".")
25+
if (j + 1) < len(p) and p[j + 1] == "*":
26+
dp[(i, j)] = dfs(i, j + 2) or (match and dfs(i + 1, j))
27+
return dp[(i, j)]
28+
if match:
29+
dp[(i, j)] = dfs(i + 1, j + 1)
30+
return dp[(i, j)]
31+
dp[(i, j)] = False
32+
return False
33+
34+
return dfs(0, 0)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._10_regular_expression_matching import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["s", "p", "expected"],
8+
argvalues=[
9+
("aa", "a", False),
10+
("aa", "a*", True),
11+
("ab", ".*", True),
12+
("aab", "c*a*b", True),
13+
("ab", ".*c", False),
14+
("a", "ab*", True),
15+
],
16+
)
17+
def test_func(s: str, p: str, expected: bool):
18+
"""Tests the solution of a LeetCode problem."""
19+
is_match = Solution().isMatch(s, p)
20+
assert is_match is expected

0 commit comments

Comments
 (0)