Mewtwo's Genetic Fusion - codepath/compsci_guides GitHub Wiki
Unit 12 Session 1 Standard (Click for link to problem statements)
Unit 12 Session 1 Advanced (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Medium
- ⏰ Time to complete: 30 mins
- 🛠️ Topics: Dynamic Programming, String Interleaving
1: U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
-
What is the goal of the problem?
- The goal is to check if
dna3
can be formed by interleaving the sequencesdna1
anddna2
.
- The goal is to check if
-
What are the base cases?
- If
dna3
's length does not equaldna1
's length +dna2
's length, returnFalse
.
- If
-
What is interleaving?
- Interleaving means merging two strings while maintaining the relative order of the characters from both strings.
HAPPY CASE
Input:
dna1 = ""aabcc""
dna2 = ""dbbca""
dna3 = ""aadbbcbcac""
Output:
True
Explanation:
The sequences can be interleaved to form the target string as shown in the problem description.
EDGE CASE
Input:
dna1 = """"
dna2 = """"
dna3 = """"
Output:
True
Explanation:
Empty strings can always be interleaved to form an empty string.
2: M-atch
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For interleaving string problems, we want to consider the following approaches:
- Dynamic Programming (DP): This problem can be solved using a DP table where we track whether the current index in
dna3
can be formed by interleaving parts ofdna1
anddna2
. - 2D Grid Traversal: We'll use a 2D DP table, where each entry
dp[i][j]
represents whether the firsti
characters ofdna1
and the firstj
characters ofdna2
can interleave to form the firsti+j
characters ofdna3
.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will use dynamic programming to solve this problem. We'll create a DP table dp[i][j]
where each entry is True
if the first i
characters of dna1
and the first j
characters of dna2
can interleave to form the first i + j
characters of dna3
.
Steps:
-
Base Case:
- If
dna1
+dna2
combined is not equal todna3
in length, returnFalse
.
- If
-
DP Table Initialization:
- Create a DP table
dp
of size(m + 1) x (n + 1)
wherem
is the length ofdna1
andn
is the length ofdna2
. - Initialize
dp[0][0]
toTrue
(an empty string can interleave to form an empty string).
- Create a DP table
-
DP Table Update:
- Update the first row and first column to check if prefixes of
dna1
anddna2
alone can form the corresponding prefix ofdna3
. - Iterate through the DP table and update each
dp[i][j]
:dp[i][j] = True
if either:dp[i-1][j] == True
anddna1[i-1] == dna3[i + j - 1]
dp[i][j-1] == True
anddna2[j-1] == dna3[i + j - 1]
- Update the first row and first column to check if prefixes of
-
Return the Result:
- The answer will be stored in
dp[m][n]
.
- The answer will be stored in
4: I-mplement
Implement the code to solve the algorithm.
def genetic_fusion(dna1, dna2, dna3):
m, n = len(dna1), len(dna2)
# If the lengths don't match, return False
if len(dna3) != m + n:
return False
# Initialize DP table
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
# Fill the first row and first column
for i in range(1, m + 1):
dp[i][0] = dp[i - 1][0] and dna1[i - 1] == dna3[i - 1]
for j in range(1, n + 1):
dp[0][j] = dp[0][j - 1] and dna2[j - 1] == dna3[j - 1]
# Fill the DP table
for i in range(1, m + 1):
for j in range(1, n + 1):
dp[i][j] = (dp[i - 1][j] and dna1[i - 1] == dna3[i + j - 1]) or (dp[i][j - 1] and dna2[j - 1] == dna3[i + j - 1])
return dp[m][n]
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
- Input:
dna1 = ""aabcc""
,dna2 = ""dbbca""
,dna3 = ""aadbbcbcac""
- Expected Output:
True
Example 2:
- Input:
dna1 = ""aabcc""
,dna2 = ""dbbca""
,dna3 = ""aadbbbaccc""
- Expected Output:
False
Example 3:
- Input:
dna1 = """"
,dna2 = """"
,dna3 = """"
- Expected Output:
True
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume m
is the length of dna1
and n
is the length of dna2
.
- Time Complexity:
O(m * n)
due to filling the DP table. - Space Complexity:
O(m * n)
for storing the DP table.