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.