We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 02f685d commit 8a4d136Copy full SHA for 8a4d136
Dynamic Programming/10-Regular-Expression-Matching/10-Regular-Expression-Matching.java
@@ -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