04. LCS of three Strings
โ GFG solution for LCS of 3 strings: dynamic programming with full 3D table, space optimized 2D, and recursion with memoization. ๐ก
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given three strings A, B, and C, the task is to find the length of the Longest Common Subsequence (LCS) present in all three strings.
A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous.
๐ Examples
Example 1
Input:
s1 = "geeks"
s2 = "geeksfor"
s3 = "geeksforgeeks"
Output: 5
Explanation:
The longest common subsequence is "geeks" (length = 5).Example 2
๐ Constraints
$1 \leq |s1|,|s2|,|s3| \leq 100$
Strings contain only lowercase English letters.
โ
My Approach
Optimized 2D DP (Rolling Arrays)
Idea:
A naive 3D DP table $dp[i][j][k]$ (size $(n1+1) \times (n2+1) \times (n3+1)$) takes $O(n1 \times n2 \times n3)$ time and space.
We can reduce space by noticing that at layer
i, we only need layeri-1to compute the current values.Maintain two 2D arrays
prev[j][k]andcurr[j][k]of size $(n2+1) \times (n3+1)$.
DP Relation:
Let
n1 = |s1|,n2 = |s2|,n3 = |s3|.For each
ifrom1ton1, and for eachjfrom1ton2, and eachkfrom1ton3:If
s1[i-1] == s2[j-1] == s3[k-1], thenOtherwise,
After filling
currfor a fixedi, swapprevandcurr, and zero outcurrfor the next iteration.
Answer:
The value
prev[n2][n3]at the end of the loops is the length of the LCS among all three strings.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: $\displaystyle O(n1 \times n2 \times n3)$, since we iterate over all
i โ [1..n1],j โ [1..n2],k โ [1..n3].Expected Auxiliary Space Complexity: $\displaystyle O(n2 \times n3)$, because we only store two 2D arrays of size $(n2+1)\times(n3+1)$ instead of a full 3D table.
๐งโ๐ป Code (C++)
๐งโ๐ป Code (Java)
๐ Code (Python)
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Letโs make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated