Search⌘ K
AI Features

Shortest Common Supersequence of Two Strings

Explore how to solve the shortest common supersequence problem for two given strings by applying dynamic programming techniques. Understand the role of longest common subsequence in building the solution and learn to implement an efficient algorithm with step-by-step explanation and code.

Problem statement

Given two strings str1 and str2, return the shortest string (supersequence) that has both str1 and str2 as subsequences. If multiple answers exist, you may return any of them.

A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.

Let’s look at the following example:

  • We have two strings, str1 = apple and str2 = les. The SCS of ...