[Algorithm] Leetcode #1092 Shortest Common Supersequence(DP/Hard/C++)

Changjin
1 min readJan 3, 2021

Problem Link: https://leetcode.com/problems/shortest-common-supersequence/

Hello! Today we’ll be looking at another variation of LCS which is to find the shortest common super sequence (SCS). Recall that a subsequence is a sequence of characters which could be formed by removing somecharacters from the original string. On the other hand, a supersequence is a sequence of characters which could be formed by adding some characters to the original string. For example, a possible supersequence of “abc” is “xaybzcxyz”.

As we want to find the shortest common supersequence of two strings, we must use all characters present in both strings. However, notice that we want to make it shortest so we dont’ have to use the repeating characters more than once.

s1 = “abc
s2 = “bce”
SCS = “abce”

In this example, there is need to use “bc” twice like “abcbce”. In other words, we want to use the “LCS” only once. Therefore, we first find the LCS and then find the SCS. Below is the code,

Time Complexity: O(n²)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Changjin
Changjin

No responses yet

Write a response