Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 1092 · LCS

Shortest Common Supersequence

LeetCode 1092 · కఠినం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

LCS table, then reconstruct while emitting skipped characters.

కఠినంStringDynamic ProgrammingLongest Common Subsequence
LeetCode లో తెరవండి ↗

నేను 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.