Skip to content

Commit bbdaa2e

Browse files
committed
Can 3rd String be formed by interleaving first and second
1 parent 8004c6e commit bbdaa2e

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

codes/interleave_string.mojo

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### Interleaving String
2+
### Determine if s3 is an interleaving of s1 and s2.
3+
4+
5+
fn is_interleave(s1: String, s2: String, s3: String) -> Bool:
6+
if len(s1) + len(s2) != len(s3):
7+
return False
8+
dp = List[List[Bool]](
9+
length=len(s1) + 1, fill=List[Bool](length=len(s2) + 1, fill=False)
10+
)
11+
dp[len(s1)][len(s2)] = True
12+
for i in range(len(s1), -1, -1):
13+
for j in range(len(s2), -1, -1):
14+
if i < len(s1) and s1[i] == s3[i + j] and dp[i + 1][j]:
15+
dp[i][j] = True
16+
if j < len(s2) and s2[j] == s3[i + j] and dp[i][j + 1]:
17+
dp[i][j] = True
18+
19+
return dp[0][0]
20+
21+
22+
from testing import assert_true, assert_false
23+
24+
25+
fn main() raises:
26+
var s1: String = "aabcc"
27+
var s2: String = "dbbca"
28+
var s3: String = "aadbbcbcac"
29+
result = is_interleave(s1, s2, s3)
30+
assert_true(result, "Assertion failed")
31+
32+
s1 = "aabcc"
33+
s2 = "dbbca"
34+
s3 = "aadbbbaccc"
35+
result = is_interleave(s1, s2, s3)
36+
assert_false(result, "Assertion failed")
37+
38+
s1 = ""
39+
s2 = ""
40+
s3 = ""
41+
result = is_interleave(s1, s2, s3)
42+
assert_true(result, "Assertion failed")

docs/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@
5353

5454
🟢 [Longest Common Subsequence Dynamic](longest_common_subeq_dyn.md) ➔ Return the length of the longest common subsequence between two strings, or 0 if none exists
5555

56-
🟢 [Longest Palindromic Substring](longest_palidromic_substr.md) ➔ Given a string s, return the longest palindromic substring therein
56+
🟢 [Longest Palindromic Substring](longest_palidromic_substr.md) ➔ Given a string s, return the longest palindromic substring therein
57+
58+
🟢 [Interleaving String](interleave_string.md) ➔ Determine if s3 is an interleaving of s1 and s2.

docs/interleave_string.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
### Interleaving String
2+
### Determine if s3 is an interleaving of s1 and s2.
3+
4+
```python
5+
6+
7+
fn is_interleave(s1: String, s2: String, s3: String) -> Bool:
8+
if len(s1) + len(s2) != len(s3):
9+
return False
10+
dp = List[List[Bool]](
11+
length=len(s1) + 1, fill=List[Bool](length=len(s2) + 1, fill=False)
12+
)
13+
dp[len(s1)][len(s2)] = True
14+
for i in range(len(s1), -1, -1):
15+
for j in range(len(s2), -1, -1):
16+
if i < len(s1) and s1[i] == s3[i + j] and dp[i + 1][j]:
17+
dp[i][j] = True
18+
if j < len(s2) and s2[j] == s3[i + j] and dp[i][j + 1]:
19+
dp[i][j] = True
20+
21+
return dp[0][0]
22+
23+
24+
from testing import assert_true, assert_false
25+
26+
27+
fn main() raises:
28+
var s1: String = "aabcc"
29+
var s2: String = "dbbca"
30+
var s3: String = "aadbbcbcac"
31+
result = is_interleave(s1, s2, s3)
32+
assert_true(result, "Assertion failed")
33+
34+
s1 = "aabcc"
35+
s2 = "dbbca"
36+
s3 = "aadbbbaccc"
37+
result = is_interleave(s1, s2, s3)
38+
assert_false(result, "Assertion failed")
39+
40+
s1 = ""
41+
s2 = ""
42+
s3 = ""
43+
result = is_interleave(s1, s2, s3)
44+
assert_true(result, "Assertion failed")
45+
46+
```
47+
48+
49+
[Source](https://github.com/ratulb/mojo_programming/blob/main/codes/interleave_string.mojo)

0 commit comments

Comments
 (0)