Tensorum
YouTubeसीखना शुरू करें
LeetCode 1092 · LCS

Shortest Common Supersequence

LeetCode 1092 · कठिन · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

LCS table, then reconstruct while emitting skipped characters.

कठिनStringDynamic ProgrammingLongest Common Subsequence

मैंने Shortest Common Supersequence (LeetCode 1092) ऐसे हल किया — LCS पैटर्न का एक कठिन प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

The shortest string containing both is built around their longest common subsequence: shared characters are written once, and every other character from either string is inserted around them. Fill the LCS table, then walk it, emitting characters as you move.

Approach

  1. 1

    Bottom-up LCS table from the ends.

  2. 2

    Walk i, j from the start: equal → emit once, advance both.

  3. 3

    Otherwise follow the larger LCS value and emit the character you skip.

  4. 4

    Append whatever remains of either string.

मैंने कैसे हल किया

LCS + reconstructionPythonO(m·n) time
class Solution:
    def shortestCommonSupersequence(self, str1, str2):
        m = len(str1)
        n = len(str2)
        dp = [[0] * (n + 1) for _ in range(m + 1)]
        for i in range(m - 1, -1, -1):
            for j in range(n - 1, -1, -1):
                if str1[i] == str2[j]:
                    dp[i][j] = 1 + dp[i + 1][j + 1]
                else:
                    dp[i][j] = max(dp[i + 1][j], dp[i][j + 1])
        i = 0
        j = 0
        result = ""
        while i < m and j < n:
            if str1[i] == str2[j]:
                result += str1[i]
                i += 1
                j += 1
            elif dp[i + 1][j] > dp[i][j + 1]:
                result += str1[i]  # in LCS we'd omit this
                i += 1
            else:
                result += str2[j]  # in LCS we'd omit this
                j += 1
        while i < m:
            result += str1[i]
            i += 1
        while j < n:
            result += str2[j]
            j += 1
        return result
स्टेप-थ्रू लोड हो रहा है…

Shortest Common Supersequence — अक्सर पूछे जाने वाले प्रश्न

Shortest Common Supersequence के पीछे intuition क्या है?

The shortest string containing both is built around their longest common subsequence: shared characters are written once, and every other character from either string is inserted around them. Fill the LCS table, then walk it, emitting characters as you move.

Shortest Common Supersequence (LeetCode 1092) कैसे हल करें?

1. Bottom-up LCS table from the ends. 2. Walk i, j from the start: equal → emit once, advance both. 3. Otherwise follow the larger LCS value and emit the character you skip. 4. Append whatever remains of either string.

Shortest Common Supersequence की time और space complexity क्या है?

Time: O(m · n)। Space: O(m · n)। (Accepted approach: LCS + reconstruction।)

Shortest Common Supersequence में आम गलतियाँ क्या हैं?

Characters you'd skip for the LCS must still appear in the supersequence.