Skip to content

Commit 65a6803

Browse files
authored
feat: add solutions to lc problem: No.44 (doocs#2470)
No.0044.Wildcard Matching
1 parent ac26e1a commit 65a6803

File tree

13 files changed

+782
-271
lines changed

13 files changed

+782
-271
lines changed

solution/0000-0099/0044.Wildcard Matching/README.md

Lines changed: 269 additions & 87 deletions
Large diffs are not rendered by default.

solution/0000-0099/0044.Wildcard Matching/README_EN.md

Lines changed: 269 additions & 87 deletions
Large diffs are not rendered by default.

solution/0000-0099/0044.Wildcard Matching/Solution.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ class Solution {
22
public:
33
bool isMatch(string s, string p) {
44
int m = s.size(), n = p.size();
5-
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));
6-
dp[0][0] = true;
7-
for (int j = 1; j <= n; ++j) {
8-
if (p[j - 1] == '*') {
9-
dp[0][j] = dp[0][j - 1];
5+
int f[m + 1][n + 1];
6+
memset(f, -1, sizeof(f));
7+
function<bool(int, int)> dfs = [&](int i, int j) {
8+
if (i >= m) {
9+
return j >= n || (p[j] == '*' && dfs(i, j + 1));
1010
}
11-
}
12-
for (int i = 1; i <= m; ++i) {
13-
for (int j = 1; j <= n; ++j) {
14-
if (s[i - 1] == p[j - 1] || p[j - 1] == '?') {
15-
dp[i][j] = dp[i - 1][j - 1];
16-
} else if (p[j - 1] == '*') {
17-
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
18-
}
11+
if (j >= n) {
12+
return false;
1913
}
20-
}
21-
return dp[m][n];
14+
if (f[i][j] != -1) {
15+
return f[i][j] == 1;
16+
}
17+
if (p[j] == '*') {
18+
f[i][j] = dfs(i + 1, j) || dfs(i, j + 1) ? 1 : 0;
19+
} else {
20+
f[i][j] = (p[j] == '?' || s[i] == p[j]) && dfs(i + 1, j + 1) ? 1 : 0;
21+
}
22+
return f[i][j] == 1;
23+
};
24+
return dfs(0, 0);
2225
}
2326
};
Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
1-
using System.Linq;
2-
31
public class Solution {
2+
private bool?[,] f;
3+
private char[] s;
4+
private char[] p;
5+
private int m;
6+
private int n;
7+
48
public bool IsMatch(string s, string p) {
5-
if (p.Count(ch => ch != '*') > s.Length)
6-
{
9+
this.s = s.ToCharArray();
10+
this.p = p.ToCharArray();
11+
m = s.Length;
12+
n = p.Length;
13+
f = new bool?[m, n];
14+
return Dfs(0, 0);
15+
}
16+
17+
private bool Dfs(int i, int j) {
18+
if (i >= m) {
19+
return j >= n || (p[j] == '*' && Dfs(i, j + 1));
20+
}
21+
if (j >= n) {
722
return false;
823
}
9-
10-
bool[,] f = new bool[s.Length + 1, p.Length + 1];
11-
bool[] d = new bool[s.Length + 1]; // d[i] means f[0, j] || f[1, j] || ... || f[i, j]
12-
for (var j = 0; j <= p.Length; ++j)
13-
{
14-
d[0] = j == 0 ? true : d[0] && p[j - 1] == '*';
15-
for (var i = 0; i <= s.Length; ++i)
16-
{
17-
if (j == 0)
18-
{
19-
f[i, j] = i == 0;
20-
continue;
21-
}
22-
23-
if (p[j - 1] == '*')
24-
{
25-
if (i > 0)
26-
{
27-
d[i] = f[i, j - 1] || d[i - 1];
28-
}
29-
f[i, j] = d[i];
30-
}
31-
else if (p[j - 1] == '?')
32-
{
33-
f[i, j] = i > 0 && f[i - 1, j - 1];
34-
}
35-
else
36-
{
37-
f[i, j] = i > 0 && f[i - 1, j - 1] && s[i - 1] == p[j - 1];
38-
}
39-
}
24+
if (f[i, j] != null) {
25+
return f[i, j].Value;
26+
}
27+
if (p[j] == '*') {
28+
f[i, j] = Dfs(i + 1, j) || Dfs(i + 1, j + 1) || Dfs(i, j + 1);
29+
} else {
30+
f[i, j] = (p[j] == '?' || s[i] == p[j]) && Dfs(i + 1, j + 1);
4031
}
41-
return f[s.Length, p.Length];
32+
return f[i, j].Value;
4233
}
43-
}
34+
}
Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
func isMatch(s string, p string) bool {
22
m, n := len(s), len(p)
3-
dp := make([][]bool, m+1)
4-
for i := range dp {
5-
dp[i] = make([]bool, n+1)
3+
f := make([][]int, m+1)
4+
for i := range f {
5+
f[i] = make([]int, n+1)
66
}
7-
dp[0][0] = true
8-
for j := 1; j <= n; j++ {
9-
if p[j-1] == '*' {
10-
dp[0][j] = dp[0][j-1]
7+
var dfs func(i, j int) bool
8+
dfs = func(i, j int) bool {
9+
if i >= m {
10+
return j >= n || p[j] == '*' && dfs(i, j+1)
1111
}
12-
}
13-
for i := 1; i <= m; i++ {
14-
for j := 1; j <= n; j++ {
15-
if s[i-1] == p[j-1] || p[j-1] == '?' {
16-
dp[i][j] = dp[i-1][j-1]
17-
} else if p[j-1] == '*' {
18-
dp[i][j] = dp[i-1][j] || dp[i][j-1]
19-
}
12+
if j >= n {
13+
return false
14+
}
15+
if f[i][j] != 0 {
16+
return f[i][j] == 1
17+
}
18+
f[i][j] = 2
19+
ok := false
20+
if p[j] == '*' {
21+
ok = dfs(i+1, j) || dfs(i+1, j+1) || dfs(i, j+1)
22+
} else {
23+
ok = (p[j] == '?' || s[i] == p[j]) && dfs(i+1, j+1)
24+
}
25+
if ok {
26+
f[i][j] = 1
2027
}
28+
return ok
2129
}
22-
return dp[m][n]
30+
return dfs(0, 0)
2331
}
Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
class Solution {
2+
private Boolean[][] f;
3+
private char[] s;
4+
private char[] p;
5+
private int m;
6+
private int n;
7+
28
public boolean isMatch(String s, String p) {
3-
int m = s.length(), n = p.length();
4-
boolean[][] dp = new boolean[m + 1][n + 1];
5-
dp[0][0] = true;
6-
for (int j = 1; j <= n; ++j) {
7-
if (p.charAt(j - 1) == '*') {
8-
dp[0][j] = dp[0][j - 1];
9-
}
9+
this.s = s.toCharArray();
10+
this.p = p.toCharArray();
11+
m = s.length();
12+
n = p.length();
13+
f = new Boolean[m][n];
14+
return dfs(0, 0);
15+
}
16+
17+
private boolean dfs(int i, int j) {
18+
if (i >= m) {
19+
return j >= n || (p[j] == '*' && dfs(i, j + 1));
20+
}
21+
if (j >= n) {
22+
return false;
23+
}
24+
if (f[i][j] != null) {
25+
return f[i][j];
1026
}
11-
for (int i = 1; i <= m; ++i) {
12-
for (int j = 1; j <= n; ++j) {
13-
if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '?') {
14-
dp[i][j] = dp[i - 1][j - 1];
15-
} else if (p.charAt(j - 1) == '*') {
16-
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
17-
}
18-
}
27+
if (p[j] == '*') {
28+
f[i][j] = dfs(i + 1, j) || dfs(i + 1, j + 1) || dfs(i, j + 1);
29+
} else {
30+
f[i][j] = (p[j] == '?' || s[i] == p[j]) && dfs(i + 1, j + 1);
1931
}
20-
return dp[m][n];
32+
return f[i][j];
2133
}
2234
}
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution:
22
def isMatch(self, s: str, p: str) -> bool:
3-
m, n = len(s), len(p)
4-
dp = [[False] * (n + 1) for _ in range(m + 1)]
5-
dp[0][0] = True
6-
for j in range(1, n + 1):
7-
if p[j - 1] == '*':
8-
dp[0][j] = dp[0][j - 1]
9-
for i in range(1, m + 1):
10-
for j in range(1, n + 1):
11-
if s[i - 1] == p[j - 1] or p[j - 1] == '?':
12-
dp[i][j] = dp[i - 1][j - 1]
13-
elif p[j - 1] == '*':
14-
dp[i][j] = dp[i - 1][j] or dp[i][j - 1]
15-
return dp[m][n]
3+
@cache
4+
def dfs(i: int, j: int) -> bool:
5+
if i >= len(s):
6+
return j >= len(p) or (p[j] == "*" and dfs(i, j + 1))
7+
if j >= len(p):
8+
return False
9+
if p[j] == "*":
10+
return dfs(i + 1, j) or dfs(i + 1, j + 1) or dfs(i, j + 1)
11+
return (p[j] == "?" or s[i] == p[j]) and dfs(i + 1, j + 1)
12+
13+
return dfs(0, 0)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function isMatch(s: string, p: string): boolean {
2+
const m = s.length;
3+
const n = p.length;
4+
const f: number[][] = Array.from({ length: m + 1 }, () =>
5+
Array.from({ length: n + 1 }, () => -1),
6+
);
7+
const dfs = (i: number, j: number): boolean => {
8+
if (i >= m) {
9+
return j >= n || (p[j] === '*' && dfs(i, j + 1));
10+
}
11+
if (j >= n) {
12+
return false;
13+
}
14+
if (f[i][j] !== -1) {
15+
return f[i][j] === 1;
16+
}
17+
if (p[j] === '*') {
18+
f[i][j] = dfs(i + 1, j) || dfs(i, j + 1) ? 1 : 0;
19+
} else {
20+
f[i][j] = (p[j] === '?' || s[i] === p[j]) && dfs(i + 1, j + 1) ? 1 : 0;
21+
}
22+
return f[i][j] === 1;
23+
};
24+
return dfs(0, 0);
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool isMatch(string s, string p) {
4+
int m = s.length(), n = p.length();
5+
bool f[m + 1][n + 1];
6+
memset(f, false, sizeof(f));
7+
f[0][0] = true;
8+
for (int j = 1; j <= n; ++j) {
9+
if (p[j - 1] == '*') {
10+
f[0][j] = f[0][j - 1];
11+
}
12+
}
13+
for (int i = 1; i <= m; ++i) {
14+
for (int j = 1; j <= n; ++j) {
15+
if (p[j - 1] == '*') {
16+
f[i][j] = f[i - 1][j] || f[i][j - 1] || f[i - 1][j - 1];
17+
} else {
18+
f[i][j] = f[i - 1][j - 1] && (p[j - 1] == '?' || s[i - 1] == p[j - 1]);
19+
}
20+
}
21+
}
22+
return f[m][n];
23+
}
24+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func isMatch(s string, p string) bool {
2+
m, n := len(s), len(p)
3+
f := make([][]bool, m+1)
4+
for i := range f {
5+
f[i] = make([]bool, n+1)
6+
}
7+
f[0][0] = true
8+
for j := 1; j <= n; j++ {
9+
if p[j-1] == '*' {
10+
f[0][j] = f[0][j-1]
11+
}
12+
}
13+
for i := 1; i <= m; i++ {
14+
for j := 1; j <= n; j++ {
15+
if p[j-1] == '*' {
16+
f[i][j] = f[i-1][j] || f[i][j-1] || f[i-1][j-1]
17+
} else {
18+
f[i][j] = f[i-1][j-1] && (p[j-1] == '?' || s[i-1] == p[j-1])
19+
}
20+
}
21+
}
22+
return f[m][n]
23+
}

0 commit comments

Comments
 (0)