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.