Skip to content

Commit 8a4d136

Browse files
committed
Time: 3 ms (84.30%), Space: 42.4 MB (71.53%) - LeetHub
1 parent 02f685d commit 8a4d136

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean isMatch(String text, String pattern) {
3+
boolean[][] dp = new boolean[text.length() + 1][pattern.length() + 1];
4+
dp[text.length()][pattern.length()] = true;
5+
6+
for (int i = text.length(); i >= 0; i--){
7+
for (int j = pattern.length() - 1; j >= 0; j--){
8+
boolean first_match = (i < text.length() &&
9+
(pattern.charAt(j) == text.charAt(i) ||
10+
pattern.charAt(j) == '.'));
11+
if (j + 1 < pattern.length() && pattern.charAt(j+1) == '*'){
12+
dp[i][j] = dp[i][j+2] || first_match && dp[i+1][j];
13+
} else {
14+
dp[i][j] = first_match && dp[i+1][j+1];
15+
}
16+
}
17+
}
18+
return dp[0][0];
19+
}
20+
}

0 commit comments

Comments
 (0)