forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterleaving String.java
More file actions
33 lines (25 loc) · 891 Bytes
/
Interleaving String.java
File metadata and controls
33 lines (25 loc) · 891 Bytes
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
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s1.length()+s2.length() != s3.length()) {
return false;
}
boolean[][] dp = new boolean[s1.length()+1][s2.length()+1];
dp[0][0] = true;
for (int i = 0; i <= s1.length(); i++){
for (int j = 0; j <= s2.length(); j++){
boolean s1Check = false;
boolean s2Check = false;
if (i > 0) {
s1Check = dp[i-1][j] && s3.charAt(i+j-1) == s1.charAt(i-1);
}
if (j > 0) {
s2Check = dp[i][j-1] && s3.charAt(i+j-1) == s2.charAt(j-1);
}
if (i>0 || j>0) {
dp[i][j] = s1Check || s2Check;
}
}
}
return dp[s1.length()][s2.length()];
}
}