Skip to content

Commit 3555172

Browse files
refactor 44
1 parent 29c9cfc commit 3555172

File tree

1 file changed

+23
-44
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-44
lines changed
Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,31 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 44. Wildcard Matching
5-
* Implement wildcard pattern matching with support for '?' and '*'.
6-
7-
'?' Matches any single character.
8-
'*' Matches any sequence of characters (including the empty sequence).
9-
10-
The matching should cover the entire input string (not partial).
11-
12-
The function prototype should be:
13-
bool isMatch(const char *s, const char *p)
14-
15-
Some examples:
16-
isMatch("aa","a") → false
17-
isMatch("aa","aa") → true
18-
isMatch("aaa","aa") → false
19-
isMatch("aa", "*") → true
20-
isMatch("aa", "a*") → true
21-
isMatch("ab", "?*") → true
22-
isMatch("aab", "c*a*b") → false
23-
*/
243
public class _44 {
254

26-
public static class Solution1 {
27-
public boolean isMatch(String s, String p) {
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;
35-
}
36-
}
5+
public static class Solution1 {
6+
public boolean isMatch(String s, String p) {
7+
boolean[][] match = new boolean[s.length() + 1][p.length() + 1];
8+
match[s.length()][p.length()] = true;
9+
for (int i = p.length() - 1; i >= 0; i--) {
10+
if (p.charAt(i) != '*') {
11+
break;
12+
} else {
13+
match[s.length()][i] = true;
14+
}
15+
}
3716

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-
}
17+
for (int i = s.length() - 1; i >= 0; i--) {
18+
for (int j = p.length() - 1; j >= 0; j--) {
19+
if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') {
20+
match[i][j] = match[i + 1][j + 1];
21+
} else if (p.charAt(j) == '*') {
22+
match[i][j] = match[i + 1][j] || match[i][j + 1];
23+
} else {
24+
match[i][j] = false;
25+
}
26+
}
27+
}
28+
return match[0][0];
4729
}
48-
}
49-
return match[0][0];
5030
}
51-
}
5231
}

0 commit comments

Comments
 (0)