Skip to content

Commit 3ff5689

Browse files
refactor 115
1 parent 2369fcc commit 3ff5689

File tree

2 files changed

+54
-31
lines changed

2 files changed

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

3-
public class _115 {
4-
/**This is a typical DP problem, illustrated in Jiuzhang.
5-
*
6-
* I've drawn out the 2d matrix on the whiteboard:
7-
*
8-
* 1. initialize a 2d matrix of size (m+1)*(n+1)
9-
* 2. initialize row 0, it should be 1,0,0,0,0... this is because when S is an empty string, only when T is empty, it could be a subsequence
10-
* 3. initialize column 0, it should be 1,1,1,1,1,1...
11-
* 4. starting from (1,1)*/
3+
/**
4+
* 115. Distinct Subsequences
5+
*
6+
* Given a string S and a string T, count the number of distinct subsequences of S which equals T. A
7+
* subsequence of a string is a new string which is formed from the original string by deleting some
8+
* (can be none) of the characters without disturbing the relative positions of the remaining
9+
* characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
10+
*
11+
* Here is an example: S = "rabbbit", T = "rabbit" Return 3.
12+
*/
1213

14+
public class _115 {
15+
public static class Solution1 {
1316
public int numDistinct(String s, String t) {
14-
int m = s.length();
15-
int n = t.length();
16-
int[][] dp = new int[m + 1][n + 1];
17+
int m = s.length();
18+
int n = t.length();
19+
int[][] dp = new int[m + 1][n + 1];
1720

18-
char[] schar = s.toCharArray();
19-
char[] tchar = t.toCharArray();
21+
char[] schar = s.toCharArray();
22+
char[] tchar = t.toCharArray();
2023

21-
for (int i = 0; i <= m; i++) {
22-
dp[i][0] = 1;
23-
}
24+
for (int i = 0; i <= m; i++) {
25+
dp[i][0] = 1;
26+
}
2427

25-
for (int j = 1; j <= n; j++) {
26-
dp[0][j] = 0;
27-
}
28+
for (int j = 1; j <= n; j++) {
29+
dp[0][j] = 0;
30+
}
2831

29-
for (int i = 1; i <= m; i++) {
30-
for (int j = 1; j <= n; j++) {
31-
if (schar[i - 1] == tchar[j - 1]) {
32-
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
33-
} else {
34-
dp[i][j] = dp[i - 1][j];
35-
}
36-
}
32+
for (int i = 1; i <= m; i++) {
33+
for (int j = 1; j <= n; j++) {
34+
if (schar[i - 1] == tchar[j - 1]) {
35+
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
36+
} else {
37+
dp[i][j] = dp[i - 1][j];
38+
}
3739
}
38-
39-
return dp[m][n];
40+
}
41+
return dp[m][n];
4042
}
41-
43+
}
4244
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._115;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _115Test {
10+
private static _115.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _115.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.numDistinct("rabbbit", "rabbit"));
20+
}
21+
}

0 commit comments

Comments
 (0)