- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterleaving_String.java
More file actions
Latest commit
50 lines (44 loc) · 1.48 KB
/
Copy pathInterleaving_String.java
File metadata and controls
50 lines (44 loc) · 1.48 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
publicclassSolution {
publicbooleanisInterleave(Strings1, Strings2, Strings3) {
if (s1.length() + s2.length() != s3.length()) {
returnfalse;
}
if (s1.length() == 0) {
returns2.equals(s3);
}
if (s2.length() == 0) {
returns1.equals(s3);
}
intlength1 = s1.length();
intlength2 = s2.length();
boolean[][] match = newboolean[length1+1][length2+1];
for (inti = 0; i <= length1; i++) {
for (intj = 0; j <= length2; j++) {
match[i][j] = false;
}
}
match[0][0] = true;
for (inti = 1; i <= length1; i++) {
if ((match[i-1][0] == true) && (s1.charAt(i-1) == s3.charAt(i-1))) {
match[i][0] = true;
}
}
for (intj = 1; j <= length2; j++) {
if ((match[0][j-1] == true) && (s2.charAt(j-1) == s3.charAt(j-1))) {
match[0][j] = true;
}
}
for (inti = 1; i <= length1; i++) {
for (intj = 1; j <= length2; j++) {
if ((match[i-1][j] == true) && (s1.charAt(i-1) == s3.charAt(i+j-1))) {
match[i][j] = true;
continue;
}
if ((match[i][j-1] == true) && (s2.charAt(j-1) == s3.charAt(i+j-1))) {
match[i][j] = true;
}
}
}
returnmatch[length1][length2];
}
}