LeetCode 1092: Shortest Common Supersequence

🚀 Day 169 of My LeetCode Journey 🚀 1092. Shortest Common Supersequence 🫧 In this problem, we are given two strings, and we need to build the shortest string that contains both of them as subsequences. ▪️ The key idea is related to the Longest Common Subsequence. If both strings share some common characters in order, we should include those characters only once in the final string. ▪️ First, I built a DP table to find the LCS length between the two strings. This helps us understand which characters are common between them. ▪️ After filling the DP table, I traversed it backwards to construct the final string. ▪️ If characters in both strings match, I add that character once to the result and move diagonally in the table. ▪️ If they don’t match, I move in the direction that gave the larger LCS value and add that character to the result. ▪️ If one string finishes before the other, I simply added the remaining characters from the other string. ▪️ In the end, I reversed the collected characters to get the shortest common supersequence, which contains both strings while keeping the length as small as possible. #LeetCode #DynamicProgramming #Strings #Python #CodingJourney #Day169 🔥

  • text

To view or add a comment, sign in

Explore content categories