Skip to content

Commit 12683a5

Browse files
左程云左程云
authored andcommitted
modify on class
1 parent 132cfea commit 12683a5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/topinterviewquestions/Problem_0044_WildcardMatching.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,27 @@ public static boolean isMatch2(String str, String pattern) {
6767
return dp[0][0];
6868
}
6969

70+
// 最终做的化简
71+
public static boolean isMatch3(String str, String pattern) {
72+
char[] s = str.toCharArray();
73+
char[] p = pattern.toCharArray();
74+
int N = s.length;
75+
int M = p.length;
76+
boolean[][] dp = new boolean[N + 1][M + 1];
77+
dp[N][M] = true;
78+
for (int pi = M - 1; pi >= 0; pi--) {
79+
dp[N][pi] = p[pi] == '*' && dp[N][pi + 1];
80+
}
81+
for (int si = N - 1; si >= 0; si--) {
82+
for (int pi = M - 1; pi >= 0; pi--) {
83+
if (p[pi] != '*') {
84+
dp[si][pi] = (p[pi] == '?' || s[si] == p[pi]) && dp[si + 1][pi + 1];
85+
} else {
86+
dp[si][pi] = dp[si][pi + 1] || dp[si + 1][pi];
87+
}
88+
}
89+
}
90+
return dp[0][0];
91+
}
92+
7093
}

0 commit comments

Comments
 (0)