Unit 12 Session 1 Standard (Click for link to problem statements)
Unit 12 Session 1 Advanced (Click for link to problem statements)
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?
dna3
can be formed by interleaving the sequences dna1
and dna2
.What are the base cases?
dna3
's length does not equal dna1
's length + dna2
's length, return False
.What is interleaving?
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.
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:
dna3
can be formed by interleaving parts of dna1
and dna2
.dp[i][j]
represents whether the first i
characters of dna1
and the first j
characters of dna2
can interleave to form the first i+j
characters of dna3
.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
.
Base Case:
dna1
+ dna2
combined is not equal to dna3
in length, return False
.DP Table Initialization:
dp
of size (m + 1) x (n + 1)
where m
is the length of dna1
and n
is the length of dna2
.dp[0][0]
to True
(an empty string can interleave to form an empty string).DP Table Update:
dna1
and dna2
alone can form the corresponding prefix of dna3
.dp[i][j]
:
dp[i][j] = True
if either:dp[i-1][j] == True
and dna1[i-1] == dna3[i + j - 1]
dp[i][j-1] == True
and dna2[j-1] == dna3[i + j - 1]
Return the Result:
dp[m][n]
.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]
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
dna1 = ""aabcc""
, dna2 = ""dbbca""
, dna3 = ""aadbbcbcac""
True
Example 2:
dna1 = ""aabcc""
, dna2 = ""dbbca""
, dna3 = ""aadbbbaccc""
False
Example 3:
dna1 = """"
, dna2 = """"
, dna3 = """"
True
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
.
O(m * n)
due to filling the DP table.O(m * n)
for storing the DP table.