Skip to content

Commit e92cc32

Browse files
committed
Added task 10.
1 parent 8440300 commit e92cc32

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package s0010.regular.expression.matching;
2+
3+
public class Solution {
4+
public boolean isMatch(String s, String p) {
5+
Boolean dp[][] = new Boolean[s.length() + 1][p.length() + 1];
6+
return isMatch(s, p, 0, 0, dp);
7+
}
8+
9+
public boolean isMatch(String s, String p, int i, int j, Boolean dp[][]) {
10+
if (i == s.length() && j == p.length()) {
11+
return true;
12+
}
13+
if (j == p.length()) {
14+
return false;
15+
}
16+
if (dp[i][j] != null) {
17+
return dp[i][j];
18+
}
19+
boolean ans = i < s.length() && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.');
20+
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
21+
return dp[i][j] = isMatch(s, p, i, j + 2, dp) || (ans && isMatch(s, p, i + 1, j, dp));
22+
}
23+
return dp[i][j] = ans && isMatch(s, p, i + 1, j + 1, dp);
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package s0010.regular.expression.matching;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void isMatch() {
11+
assertThat(new Solution().isMatch("aa", "a"), equalTo(false));
12+
}
13+
}

0 commit comments

Comments
 (0)