Added Longest Common Subsequence solution to C++ folder #74
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Statement: Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
Solution: To find the longest common subsequence in two strings :
We first make a dynamic programming table c, using a 2D array.
The first row and first column are initialized to zero.
If characters match the LCS increases by 1 (we increment the value of [i-1][j-1].
Otherwise, it takes the maximum LCS length and skips the character.
The final length is stored in c and that is what is returned.
Output:
Test Case 1: text1 = "abcde"; text2 = "ace": Output: 3
Test Case 2: text1 = "abc"; text2 = "abc": Output: 3
Test Case 3: text1 = "abc"; text2 = "def": Output: 0
I would really appreciate it if you accept this pull request and assign it under the appropriate Hacktoberfest 2024 tags before merging.
Thankyou!