FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-algorithms/src/InterleavingString.java at master · anishLearnsToCode/leetcode-algorithms · GitHub
anishLearnsToCode
/
leetcode-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
17
Star
98
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-algorithms
/
src
/
InterleavingString.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (30 loc) · 1.27 KB
Breadcrumbs
leetcode-algorithms
/
src
/
InterleavingString.java
Copy path
File metadata and controls
34 lines (30 loc) · 1.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// https://leetcode.com/problems/interleaving-string
// T: O(n * m)
// S: O(min(m, n))
public
class
InterleavingString
{
public
boolean
isInterleave
(
String
s1
,
String
s2
,
String
s3
) {
if
(
s1
.
length
() +
s2
.
length
() !=
s3
.
length
())
return
false
;
if
(
s1
.
length
() <
s2
.
length
()) {
String
temp
=
s1
;
s1
=
s2
;
s2
=
temp
;
}
final
int
rows
=
s1
.
length
() +
1
,
columns
=
s2
.
length
() +
1
;
final
boolean
[]
dp
=
new
boolean
[
columns
];
for
(
int
row
=
0
;
row
<
rows
;
row
++) {
for
(
int
column
=
0
;
column
<
columns
;
column
++) {
if
(
row
==
0
&&
column
==
0
) {
dp
[
column
] =
true
;
}
else
if
(
row
==
0
) {
dp
[
column
] =
dp
[
column
-
1
] &&
s2
.
charAt
(
column
-
1
) ==
s3
.
charAt
(
row
+
column
-
1
);
}
else
if
(
column
==
0
) {
dp
[
column
] =
dp
[
column
] &&
s1
.
charAt
(
row
-
1
) ==
s3
.
charAt
(
row
+
column
-
1
);
}
else
{
dp
[
column
] = (
s1
.
charAt
(
row
-
1
) ==
s3
.
charAt
(
row
+
column
-
1
) &&
dp
[
column
])
|| (
s2
.
charAt
(
column
-
1
) ==
s3
.
charAt
(
row
+
column
-
1
) &&
dp
[
column
-
1
]);
}
}
}
return
dp
[
columns
-
1
];
}
}
Back
|
FazBrowse Home
|
New Git URL