Skip to content

1092. Shortest Common Supersequence #1373

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the shortest common supersequence (SCS) of two given strings. The SCS is the shortest string that contains both input strings as subsequences. The approach involves using dynamic programming (DP) to determine the longest common subsequence (LCS) and then constructing the SCS by merging the two input strings based on the LCS.

Approach

  1. Dynamic Programming (DP) Table Construction:

    • Create a DP table where dp[i][j] represents the length of the LCS of the substrings str1[0..i-1] and str2[0..j-1].
    • Fill the DP table by comparing characters of the two strings and using previously computed values to build up the solution.
  2. Backtracking to Construct SCS:

    • Use the DP table to bac…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Feb 28, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Feb 28, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants