Skip to content

Commit 0d7cc16

Browse files
committed
dp
1 parent 43c9d29 commit 0d7cc16

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package DistinctSubsequences;
2+
3+
/**
4+
* User: Danyang
5+
* Date: 1/28/2015
6+
* Time: 18:46
7+
*
8+
* Given a string S and a string T, count the number of distinct subsequences of T in S.
9+
10+
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of
11+
the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of
12+
"ABCDE" while "AEC" is not).
13+
14+
Here is an example:
15+
S = "rabbbit", T = "rabbit"
16+
17+
Return 3.
18+
*/
19+
public class Solution {
20+
/**
21+
* let f[i][j] represents the num of distinct subsequence of b[0..j] in a[0..i]
22+
* f[i][j] = f[i-1][j-1] + f[i-1][j] if a[i]=b[i]
23+
* = f[i-1][j] otherwise
24+
* @param S
25+
* @param T
26+
* @return
27+
*/
28+
public int numDistinct(String S, String T) {
29+
int m = S.length();
30+
int n = T.length();
31+
int[][] f = new int[m+1][n+1];
32+
for(int i=0; i<m+1; i++)
33+
f[i][0] = 1;
34+
for(int i=1; i<m+1; i++) {
35+
for(int j=1; j<n+1; j++) {
36+
if(S.charAt(i-1)==T.charAt(j-1))
37+
f[i][j] = f[i-1][j-1];
38+
f[i][j] += f[i-1][j];
39+
}
40+
}
41+
return f[m][n];
42+
}
43+
}

src/InterleavingString/Solution.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package InterleavingString;
2+
3+
/**
4+
* User: Danyang
5+
* Date: 1/28/2015
6+
* Time: 17:11
7+
*
8+
* Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
9+
10+
For example,
11+
Given:
12+
s1 = "aabcc",
13+
s2 = "dbbca",
14+
15+
When s3 = "aadbbcbcac", return true.
16+
When s3 = "aadbbbaccc", return false.
17+
*/
18+
public class Solution {
19+
/**
20+
* dp
21+
* f[i, j] represents s3[0..i+j] is interleaved from s1[0..i] and s2[0..j]
22+
* f[i, j] = f[i-1, j] if s1[i] = s3[i+j]
23+
* = f[i, j-1] if s2[j] = s3[i+j]
24+
*
25+
* Notice:
26+
* 1. initial condition
27+
* 2. s3[i+j-1] rather than s3[i+j-2]
28+
* @param s1
29+
* @param s2
30+
* @param s3
31+
* @return
32+
*/
33+
public boolean isInterleave(String s1, String s2, String s3) {
34+
if(s1.length()+s2.length()!=s3.length())
35+
return false;
36+
37+
boolean[][] f = new boolean[s1.length()+1][s2.length()+1];
38+
f[0][0] = true;
39+
for(int i=1; i<s1.length()+1; i++)
40+
f[i][0] = f[i-1][0] && s1.charAt(i-1)==s3.charAt(i-1);
41+
for(int j=1; j<s2.length()+1; j++)
42+
f[0][j] = f[0][j-1] && s2.charAt(j-1)==s3.charAt(j-1);
43+
44+
for(int i=1; i<s1.length()+1; i++)
45+
for(int j=1; j<s2.length()+1; j++) {
46+
if(!f[i][j] && s1.charAt(i-1)==s3.charAt(i+j-1))
47+
f[i][j] = f[i-1][j];
48+
if(!f[i][j] && s2.charAt(j-1)==s3.charAt(i+j-1))
49+
f[i][j] = f[i][j-1];
50+
}
51+
52+
return f[s1.length()][s2.length()];
53+
}
54+
55+
public static void main(String[] args) {
56+
assert new Solution().isInterleave("aabcc", "dbbca", "aadbbcbcac");
57+
}
58+
}

0 commit comments

Comments
 (0)