forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path97-Interleaving-String.java
More file actions
34 lines (30 loc) · 873 Bytes
/
97-Interleaving-String.java
File metadata and controls
34 lines (30 loc) · 873 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
34
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[s1.length()][s2.length()] = true;
for (int i = dp.length - 1; i >= 0; i--) for (
int j = dp[0].length - 1;
j >= 0;
j--
) {
if (
i < s1.length() &&
s1.charAt(i) == s3.charAt(i + j) &&
dp[i + 1][j]
) {
dp[i][j] = true;
}
if (
j < s2.length() &&
s2.charAt(j) == s3.charAt(i + j) &&
dp[i][j + 1]
) {
dp[i][j] = true;
}
}
return dp[0][0];
}
}