Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
29 lines (26 loc) · 992 Bytes

File metadata and controls

29 lines (26 loc) · 992 Bytes
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
/**
* 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 {
public boolean isInterleave(String s1, String s2, String s3) {
if (s1.length() + s2.length() != s3.length())
return false;
boolean[][] mat = new boolean[s1.length() + 1][s2.length() + 1];
mat[0][0] = true;
for (int i = 1; i <= s1.length(); ++i)
mat[i][0] = mat[i - 1][0] && (s3.charAt(i - 1) == s1.charAt(i - 1));
for (int i = 1; i <= s2.length(); ++i)
mat[0][i] = mat[0][i - 1] && (s3.charAt(i - 1) == s2.charAt(i - 1));
for (int i = 1; i <= s1.length(); ++i)
for (int j = 1; j <= s2.length(); ++j)
mat[i][j] = (mat[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i
+ j - 1))
|| (mat[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i
+ j - 1));
return mat[s1.length()][s2.length()];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.