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?
HAPPY CASE
Input:
katara_moves = ""waterbend"", toph_moves = ""earthbend""
Output:
5
Explanation:
The longest common subsequence is ""bend"" and its length is 5.
EDGE CASE
Input:
katara_moves = ""fire"", toph_moves = ""air""
Output:
0
Explanation:
There is no common subsequence between ""fire"" and ""air"", so the result is 0.
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 longest common subsequence problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use dynamic programming to calculate the length of the longest common subsequence between two strings. We'll build a DP table where dp[i][j]
represents the length of the longest common subsequence between the first i
characters of katara_moves
and the first j
characters of toph_moves
.
Initialization:
(m + 1) x (n + 1)
where m
is the length of katara_moves
and n
is the length of toph_moves
. Initialize all values to 0
.Filling the DP Table:
i
from 1 to m
and each j
from 1 to n
:
katara_moves[i - 1] == toph_moves[j - 1]
, then dp[i][j] = dp[i - 1][j - 1] + 1
(we found a matching character).dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
(choose the longest subsequence found so far).Return the Result:
dp[m][n]
will give the length of the longest common subsequence.Implement the code to solve the algorithm.
def training_synchronization(katara_moves, toph_moves):
m, n = len(katara_moves), len(toph_moves)
# Create a DP table with dimensions (m+1) x (n+1)
dp = [[0] * (n + 1) for _ in range(m + 1)]
# Fill the DP table
for i in range(1, m + 1):
for j in range(1, n + 1):
if katara_moves[i - 1] == toph_moves[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
# The value in dp[m][n] is the length of the longest common subsequence
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:
katara_moves = ""waterbend"", toph_moves = ""earthbend""
5
Example 2:
katara_moves = ""bend"", toph_moves = ""bend""
4
Example 3:
katara_moves = ""fire"", toph_moves = ""air""
0
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume m
is the length of katara_moves
and n
is the length of toph_moves
.
O(m * n)
because we fill a DP table of size (m+1) x (n+1)
.O(m * n)
to store the DP table.