1.6 KiB
Technique
The technique used here is a bottom-up dynamic programming approach. The main steps are as follows:
-
Initialization: A 2D DP table
dp
is created with dimensions(text1.len() + 1) x (text2.len() + 1)
. The extra row and column are for the base case when one of the strings is empty. -
Conversion to Character Vectors: The strings
text1
andtext2
are converted to vectors of characters. This is done because strings in Rust are not indexable due to their UTF-8 encoding. Converting them to character vectors allows for easier indexing. -
Filling the DP Table: The outer loop iterates over
text1
in reverse order, and the inner loop iterates overtext2
in reverse order. For each pair of characterstext1[i]
andtext2[j]
, it checks if they are equal. If they are equal, it increments the length of the current longest common subsequence, which is stored indp[i+1][j+1]
, by 1 and stores it indp[i][j]
. If they are not equal, it takes the maximum length of the common subsequence found so far, which is the maximum ofdp[i][j+1]
anddp[i+1][j]
, and stores it indp[i][j]
. -
Result: After all iterations,
dp[0][0]
will have the length of the longest common subsequence oftext1
andtext2
.
Time and Space Complexity Analysis
Time Complexity: The time complexity of this approach is O(m*n), where m and n are the lengths of text1
and text2
, respectively. This is because each cell in the DP table is filled exactly once.
Space Complexity: The space complexity is also O(mn) due to the 2D DP table. Each cell in the table stores an integer, and there are (m+1)(n+1) cells in total.