Skip to content

Commit 234decc

Browse files
committed
leetcode
1 parent 937bc71 commit 234decc

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
3+
-* 97. Interleaving String *-
4+
5+
6+
Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
7+
8+
An interleaving of two strings s and t is a configuration where s and t are divided into n and m
9+
substrings
10+
respectively, such that:
11+
12+
s = s1 + s2 + ... + sn
13+
t = t1 + t2 + ... + tm
14+
|n - m| <= 1
15+
The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...
16+
Note: a + b is the concatenation of strings a and b.
17+
18+
19+
20+
Example 1:
21+
22+
23+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
24+
Output: true
25+
Explanation: One way to obtain s3 is:
26+
Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a".
27+
Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac".
28+
Since s3 can be obtained by interleaving s1 and s2, we return true.
29+
Example 2:
30+
31+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
32+
Output: false
33+
Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.
34+
Example 3:
35+
36+
Input: s1 = "", s2 = "", s3 = ""
37+
Output: true
38+
39+
40+
Constraints:
41+
42+
0 <= s1.length, s2.length <= 100
43+
0 <= s3.length <= 200
44+
s1, s2, and s3 consist of lowercase English letters.
45+
46+
47+
Follow up: Could you solve it using only O(s2.length) additional memory space?
48+
49+
50+
*/
51+
52+
class A {
53+
bool isInterleave(String s1, String s2, String s3) {
54+
final int m = s1.length, n = s2.length, k = s3.length;
55+
if (m + n != k) {
56+
return false;
57+
}
58+
59+
int dp1 = 1;
60+
for (int i = 1; i <= n; i++) {
61+
if (s2[i - 1] == s3[i - 1] && dp1 & 1 == 1) {
62+
dp1 = (dp1 << 1) + 1;
63+
} else {
64+
dp1 = dp1 << 1;
65+
}
66+
}
67+
68+
for (int j = 1; j <= m; j++) {
69+
int dp2 = 0;
70+
if (s1[j - 1] == s3[j - 1] && dp1 & (1 << n) != 0) {
71+
dp2 = 1;
72+
}
73+
for (int i = 1; i <= n; i++) {
74+
if ((s1[j - 1] == s3[j + i - 1] && dp1 & (1 << (n - i)) != 0) ||
75+
(s2[i - 1] == s3[j + i - 1] && dp2 & 1 == 1)) {
76+
dp2 = (dp2 << 1) + 1;
77+
} else {
78+
dp2 = dp2 << 1;
79+
}
80+
}
81+
dp1 = dp2;
82+
}
83+
84+
return dp1 & 1 == 1;
85+
}
86+
}
87+
88+
class Solution {
89+
bool solve(
90+
String s1, String s2, String s3, int ind1, int ind2, List<List<int>> dp) {
91+
if (ind1 + ind2 == s3.length) return true;
92+
if (dp[ind1][ind2] != -1) return dp[ind1][ind2] == 1;
93+
bool ans = false;
94+
95+
if (ind1 < s1.length && s1[ind1] == s3[ind1 + ind2]) {
96+
ans |= solve(s1, s2, s3, ind1 + 1, ind2, dp);
97+
}
98+
99+
if (ind2 < s2.length && s2[ind2] == s3[ind1 + ind2]) {
100+
ans |= solve(s1, s2, s3, ind1, ind2 + 1, dp);
101+
}
102+
103+
dp[ind1][ind2] = ans ? 1 : 0;
104+
return ans;
105+
}
106+
107+
bool isInterleave(String s1, String s2, String s3) {
108+
if (s1.length + s2.length != s3.length) {
109+
return false;
110+
}
111+
112+
// int[][] dp = new int[s1.length + 1][s2.length + 1];
113+
final List<List<int>> dp = List.filled(s1.length + 1, 0)
114+
.map((e) => List.filled(s2.length + 1, 0))
115+
.toList();
116+
for (int i = 0; i <= s1.length; i++) {
117+
// Arrays.fill(dp[i], -1);
118+
List.filled(dp[i].length, -1);
119+
}
120+
121+
return solve(s1, s2, s3, 0, 0, dp);
122+
}
123+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
func isInterleave(s1 string, s2 string, s3 string) bool {
4+
m, n, k := len(s1), len(s2), len(s3)
5+
if m+n != k {
6+
return false
7+
}
8+
9+
dp1 := 1
10+
for i := 1; i <= n; i++ {
11+
if s2[i-1] == s3[i-1] && dp1&1 == 1 {
12+
dp1 = (dp1 << 1) + 1
13+
} else {
14+
dp1 = dp1 << 1
15+
}
16+
}
17+
18+
for j := 1; j <= m; j++ {
19+
dp2 := 0
20+
if s1[j-1] == s3[j-1] && dp1&(1<<n) != 0 {
21+
dp2 = 1
22+
}
23+
for i := 1; i <= n; i++ {
24+
if (s1[j-1] == s3[j+i-1] && dp1&(1<<(n-i)) != 0) ||
25+
(s2[i-1] == s3[j+i-1] && dp2&1 == 1) {
26+
dp2 = (dp2 << 1) + 1
27+
} else {
28+
dp2 = dp2 << 1
29+
}
30+
}
31+
dp1 = dp2
32+
}
33+
34+
return dp1&1 == 1
35+
}

InterleavingString/interleaving_string.md

Whitespace-only changes.

0 commit comments

Comments
 (0)