Skip to content

Commit 100ce1d

Browse files
[N-0] refactor 10
1 parent afa3a47 commit 100ce1d

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

src/main/java/com/fishercoder/solutions/_10.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,34 @@
2525

2626
public class _10 {
2727

28-
public boolean isMatch(String s, String p) {
29-
if (s == null || p == null) {
30-
return false;
31-
}
32-
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
33-
dp[0][0] = true;
34-
for (int i = 0; i < p.length(); i++) {
35-
if (p.charAt(i) == '*' && dp[0][i - 1]) {
36-
dp[0][i + 1] = true;
28+
public static class Solution1 {
29+
public boolean isMatch(String s, String p) {
30+
if (s == null || p == null) {
31+
return false;
3732
}
38-
}
39-
for (int i = 0; i < s.length(); i++) {
40-
for (int j = 0; j < p.length(); j++) {
41-
if (p.charAt(j) == '.' || p.charAt(j) == s.charAt(i)) {
42-
dp[i + 1][j + 1] = dp[i][j];
33+
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
34+
dp[0][0] = true;
35+
for (int i = 0; i < p.length(); i++) {//here's the p's length, not s's
36+
if (p.charAt(i) == '*' && dp[0][i - 1]) {
37+
dp[0][i + 1] = true;//here's y axis should be i+1
4338
}
44-
if (p.charAt(j) == '*') {
45-
if (p.charAt(j - 1) != s.charAt(i) && p.charAt(j - 1) != '.') {
46-
dp[i + 1][j + 1] = dp[i + 1][j - 1];
47-
} else {
48-
dp[i + 1][j + 1] = (dp[i + 1][j] || dp[i][j + 1] || dp[i + 1][j - 1]);
39+
}
40+
for (int i = 0; i < s.length(); i++) {
41+
for (int j = 0; j < p.length(); j++) {
42+
if (p.charAt(j) == '.' || p.charAt(j) == s.charAt(i)) {
43+
dp[i + 1][j + 1] = dp[i][j];
44+
}
45+
if (p.charAt(j) == '*') {
46+
if (p.charAt(j - 1) != s.charAt(i) && p.charAt(j - 1) != '.') {
47+
dp[i + 1][j + 1] = dp[i + 1][j - 1];
48+
} else {
49+
dp[i + 1][j + 1] = (dp[i + 1][j] || dp[i][j + 1] || dp[i + 1][j - 1]);
50+
}
4951
}
5052
}
5153
}
54+
return dp[s.length()][p.length()];
5255
}
53-
return dp[s.length()][p.length()];
5456
}
5557

5658
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._10;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _10Test {
10+
private static _10.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _10.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.isMatch("", ""));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.isMatch("aa", "a"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.isMatch("aab", "c*a*b"));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)