Shortest common supersequence

Dynamic Programming DP on strings Hard
  • Fun Fact: The concept of finding the shortest common supersequence, as this problem explores, has applications in the field of bioinformatics
  • It is used in DNA sequence alignment, which is a way of arranging the sequences of DNA, RNA, or protein to identify regions of similarity
  • These methods are used to explore functional, structural, or evolutionary relationships between the sequences
  • In software development, algorithms solving this problem are used in applications that aim to align, compare, and analyze genetic material

Given two strings str1 and str2, find the shortest common supersequence.


The shortest common supersequence is the shortest string that contains both str1 and str2 as subsequences.

Examples:

Input: str1 = "mno", str2 = "nop"

Output: "mnop"

Explanation: The shortest common supersequence is "mnop". It contains "mno" as the first three characters and "nop" as the last three characters, thus including both strings as subsequences.

Input: str1 = "dynamic", str2 = "program"

Output: "dynprogramic"

Explanation: The shortest common supersequence is "dynamprogramic". It includes all characters from both "dynamic" and "program", with minimal overlap. For example, "dynamic" appears as "dynam...ic" and "program" appears as "...program..." within "dynamprogramic".

Input: str1 = "apple", str2 = "orange"

Constraints

  • 1 <= str1.length, str2.length <= 1000
  • str1 and str2 consist of lowercase English letters.

Hints

  • "The Shortest Common Supersequence (SCS) must contain both str1 and str2 as subsequences. The LCS of str1 and str2 represents the common part that appears only once in the SCS. The extra characters in str1 and str2 (not in the LCS) must be included."
  • "SCS Length=len(str1)+len(str2)−LCS(str1,str2) Compute LCS(str1, str2). Use it to construct the SCS string."

Company Tags

Stripe Deloitte eBay Byju's Rockstar Games Twilio Dropbox Freshworks Boston Consulting Group Qualcomm Bloomberg Zynga Texas Instruments Rakuten Ubisoft OYO Rooms Robinhood Flipkart Oracle Airbnb MongoDB Bungie Electronic Arts Salesforce HashiCorp Google Microsoft Amazon Meta Apple Netflix Adobe