forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_dp_n2.java
43 lines (35 loc) · 1.16 KB
/
AC_dp_n2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dp_n2.java
* Create Date: 2015-03-08 22:53:30
* Descripton:
*/
import java.util.*;
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
if (len1 + len2 != len3) {
return false;
}
boolean[] dp = new boolean[len2 + 1];
dp[0] = true;
for (int j = 1; j <= len2; ++j) {
dp[j] = dp[j - 1] && s2.charAt(j - 1) == s3.charAt(j - 1);
}
for (int i = 1; i <= len1; ++i) {
dp[0] = dp[0] && s1.charAt(i - 1) == s3.charAt(i - 1);
for (int j = 1; j <= len2; ++j) {
dp[j] = (dp[j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) ||
(dp[j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
}
return dp[len2];
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {1, 2, 3, 4};
System.out.println("no case");
}
}