8000 refactor 44 · ashish53v/Leetcode@766f49b · GitHub
[go: up one dir, main page]

Skip to content

Commit 766f49b

Browse files
refactor 44
1 parent d955d2b commit 766f49b

File tree

2 files changed

+73
-21
lines changed

2 files changed

+73
-21
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,30 @@ bool isMatch(const char *s, const char *p)
2323
*/
2424
public class _44 {
2525

26+
public static class Solution1 {
2627
public boolean isMatch(String s, String p) {
27-
boolean[][] match = new boolean[s.length() + 1][p.length() + 1];
28-
match[s.length()][p.length()] = true;
29-
for (int i = p.length() - 1; i >= 0; i--) {
30-
if (p.charAt(i) != '*') {
31-
break;
32-
} else {
33-
match[s.length()][i] = true;
34-
}
28+
boolean[][] match = new boolean[s.length() + 1][p.length() + 1];
29+
match[s.length()][p.length()] = true;
30+
for (int i = p.length() - 1; i >= 0; i--) {
31+
if (p.charAt(i) != '*') {
32+
break;
33+
} else {
34+
match[s.length()][i] = true;
3535
}
36-
37-
for (int i = s.length() - 1; i >= 0; i--) {
38-
for (int j = p.length() - 1; j >= 0; j--) {
39-
if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') {
40-
match[i][j] = match[i + 1][j + 1];
41-
} else if (p.charAt(j) == '*') {
42-
match[i][j] = match[i + 1][j] || match[i][j + 1];
43-
} else {
44-
match[i][j] = false;
45-
}
46-
}
36+
}
37+
38+
for (int i = s.length() - 1; i >= 0; i--) {
39+
for (int j = p.length() - 1; j >= 0; j--) {
40+
if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') {
41+
match[i][j] = match[i + 1][j + 1];
42+
} else if (p.charAt(j) == '*') {
43+
match[i][j] = match[i + 1][j] || match[i][j + 1];
44+
} else {
45+
match[i][j] = false;
46+
}
4747
}
48-
return match[0][0];
48+
}
49+
return match[0][0];
4950
}
50-
51+
}
5152
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._44;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _44Test {
10+
private static _44.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _44.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(false, solution1.isMatch("aa", "a"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(true, solution1.isMatch("aa", "aa"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(false, solution1.isMatch("aaa", "aa"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(true, solution1.isMatch("aa", "*"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(true, solution1.isMatch("aa", "a*"));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(true, solution1.isMatch("ab", "?*"));
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals(false, solution1.isMatch("aab", "c*a*b"));
50+
}
51+
}

0 commit comments

Comments
 (0)
0