-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathInterleavingString.java
More file actions
52 lines (46 loc) · 1.39 KB
/
InterleavingString.java
File metadata and controls
52 lines (46 loc) · 1.39 KB
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
44
45
46
47
48
49
50
51
52
package algorithm.lc;
/**
* Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
*
* For example, Given: s1 = "aabcc", s2 = "dbbca",
*
* When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false.
*
*/
public class InterleavingString {
// 2D DP
// O(mn) space, O(mn) time
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
// Start typing your Java solution below
// DO NOT write main() function
if (s3.length() != s1.length() + s2.length()) {
return false;
}
boolean[][] match = new boolean[s1.length() + 1][s2.length() + 1];
match[0][0] = true;
int i = 1;
while (i < s1.length() + 1 && s1.charAt(i - 1) == s3.charAt(i - 1)) {
match[i][0] = true;
++i;
}
int j = 1;
while (j < s2.length() + 1 && s2.charAt(j - 1) == s3.charAt(j - 1)) {
match[0][j] = true;
++j;
}
for (i = 1; i < s1.length() + 1; ++i) {
for (j = 1; j < s2.length() + 1; ++j) {
char c = s3.charAt(i + j - 1);
if (c == s1.charAt(i - 1)) { // s1[i] can be used
match[i][j] |= match[i - 1][j];
}
if (c == s2.charAt(j - 1)) { // s2[j] can be used
match[i][j] |= match[i][j - 1];
}
}
}
return match[s1.length()][s2.length()];
}
}
}