Skip to content

Commit 06e2603

Browse files
authored
Merge pull request #1259 from larkloss/master
436-Week 08
2 parents 33a9f50 + 78e94ee commit 06e2603

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
public boolean isMatch(String s, String p) {
3+
if (s == null || p == null) {
4+
return false;
5+
}
6+
boolean[][] memo = new boolean[s.length()][p.length()];
7+
boolean[][] visited = new boolean[s.length()][p.length()];
8+
return isMatchHelper(s, 0, p, 0, memo, visited);
9+
}
10+
private boolean charMatch(char sChar, char pChar) {
11+
return (sChar == pChar || pChar == '?');
12+
}
13+
private boolean allStar(String p, int pIndex) {
14+
for (int i = pIndex; i < p.length(); i++) {
15+
if (p.charAt(i) != '*') {
16+
return false;
17+
}
18+
}
19+
return true;
20+
}
21+
private boolean isMatchHelper(String s, int sIndex,
22+
String p, int pIndex,
23+
boolean[][] memo,
24+
boolean[][] visited) {
25+
if (pIndex == p.length()) {
26+
return sIndex == s.length();
27+
}
28+
if (sIndex == s.length()) {
29+
return allStar(p, pIndex);
30+
}
31+
if (visited[sIndex][pIndex]) {
32+
return memo[sIndex][pIndex];
33+
}
34+
35+
char sChar = s.charAt(sIndex);
36+
char pChar = p.charAt(pIndex);
37+
boolean match;
38+
39+
if (pChar == '*') {
40+
match = isMatchHelper(s, sIndex, p, pIndex + 1, memo, visited) ||
41+
isMatchHelper(s, sIndex + 1, p, pIndex, memo, visited);
42+
} else {
43+
match = charMatch(sChar, pChar) &&
44+
isMatchHelper(s, sIndex + 1, p, pIndex + 1, memo, visited);
45+
}
46+
47+
visited[sIndex][pIndex] = true;
48+
memo[sIndex][pIndex] = match;
49+
return match;
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int numDecodings(String s) {
3+
if (s.length() == 0) {
4+
return 0;
5+
}
6+
char[] c = s.toCharArray();
7+
int[] f = new int[s.length() + 1];
8+
f[0] = 1;
9+
for (int i = 1; i <= s.length(); ++i) {
10+
if (c[i - 1] != '0') {
11+
f[i] += f[i - 1];
12+
}
13+
if (i >= 2) {
14+
int val = (c[i - 2] - '0') * 10 + c[i - 1] - '0';
15+
if (10 <= val && val <= 26) {
16+
f[i] += f[i - 2];
17+
}
18+
}
19+
}
20+
return f[s.length()];
21+
}
22+
//in: 0 2 2 6
23+
//ou: 1 1 2 4
24+
}

0 commit comments

Comments
 (0)