Skip to content

Commit 4994ba1

Browse files
add 10
1 parent dded362 commit 4994ba1

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LeetCode
1111
|0005|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | c | [c++](./src/0005-Longest-Palindromic-Substring/0005.cpp) |[python](./src/0005-Longest-Palindromic-Substring/0005.py)|||Medium|
1212
|0006|[ZigZag Conversion](https://leetcode.com/problems/third-maximum-number/) | c | [c++]() |python|||Medium|
1313
|0007|[Reverse Integer](https://leetcode.com/problems/arithmetic-slices/) | c | [c++]() |python|||Easy|
14-
|0008|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | c | [c++]() |python|||Medium|
14+
|0008|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | c | [c++](./src/0010-Regular-Expression-Matching/0010.cpp) |[python](./src/0010-Regular-Expression-Matching/0010.py)|||Medium|
1515
|0009|[Palindrome Number](https://leetcode.com/problems/split-array-largest-sum/) | c | [c++]() |python|||Easy|
1616
|0010|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching) | c | [c++]() |python|||Hard|
1717
|0011|[Container With Most Water](https://leetcode.com/problems/queue-reconstruction-by-height/) | c | [c++](./src/0011-Container-With-Most-Water/0011.cpp)|[python](./src/0011-Container-With-Most-Water/0011.cpp)|||Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <string>
2+
#include <iostream>
3+
using namespace std;
4+
5+
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
6+
class Solution
7+
{
8+
public:
9+
bool isMatch(string s, string p)
10+
{
11+
int m = s.length(), n = p.length();
12+
vector<vector<bool>> mem(m + 1, vector<bool> (n + 1, false));
13+
mem[0][0] = true;
14+
for (int j = 1; j <= n; j++) mem[0][j] = p[j-1] == '*' && mem[0][j-2];
15+
for (int i = 1; i <= m; i++)
16+
{
17+
for (int j = 1; j <= n; j++)
18+
{
19+
if (p[j - 1] == '*')
20+
mem[i][j] = mem[i][j - 2] ||
21+
(i > 0 && (s[i - 1] == p[j - 2] ||
22+
p[j - 2] == '.') && mem[i - 1][j]);
23+
else mem[i][j] = i > 0 &&
24+
mem[i - 1][j - 1] &&
25+
(s[i - 1] == p[j - 1] || p[j - 1] == '.');
26+
}
27+
}
28+
return mem[m][n];
29+
}
30+
};
31+
int main()
32+
{
33+
string s = "baccbbcbcacacbbc";
34+
string p = "c*.*b*c*ba*b*b*.a*";
35+
cout << Solution().isMatch(s, p) << endl;
36+
return 0;
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def isMatch(self, s, p):
3+
mem = {}
4+
def _isMatch(i, j):
5+
if (i, j) not in mem:
6+
if j == len(p):
7+
result = i == len(s)
8+
else:
9+
first_match = i < len(s) and p[j] in (s[i], '.')
10+
if j + 1 < len(p) and p[j+1] == '*':
11+
result = _isMatch(i, j+2) or (first_match and _isMatch(i+1, j))
12+
else:
13+
result = first_match and _isMatch(i+1, j+1)
14+
mem[i, j] = result
15+
return mem[i, j]
16+
17+
return _isMatch(0,0)
18+
19+
20+
if __name__ == "__main__":
21+
s = "baccbbcbcacacbbc"
22+
p = "c*.*b*c*ba*b*b*.a*"
23+
print(Solution().isMatch(s, p))

0 commit comments

Comments
 (0)